TypeError:"DataFrame"对象不可调用 [英] TypeError: 'DataFrame' object is not callable

查看:448
本文介绍了TypeError:"DataFrame"对象不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为计算方差编写了这些程序

I've programmed these for calculating Variance

import pandas as pd
import xlrd
import numpy as np
import matplotlib.pyplot as plt


credit_card=pd.read_csv("default_of_credit_card_clients_Data.csv",skiprows=1)

print(credit_card.head())
for col in credit_card:
    var[col]=np.var(credit_card(col))

print(var)

我遇到此错误

回溯(最近一次通话最近):文件"C:/Python34/project.py", 第11行,在 var [col] = np.var(credit_card(col))TypeError:"DataFrame"对象不可调用

Traceback (most recent call last): File "C:/Python34/project.py", line 11, in var[col]=np.var(credit_card(col)) TypeError: 'DataFrame' object is not callable

一个解决方案将不胜感激.

A solution will be appreciated.

推荐答案

似乎您需要

It seems you need DataFrame.var:

默认情况下被N-1标准化.可以使用ddof参数更改

Normalized by N-1 by default. This can be changed using the ddof argument

var1 = credit_card.var()

示例:

#random dataframe
np.random.seed(100)
credit_card = pd.DataFrame(np.random.randint(10, size=(5,5)), columns=list('ABCDE'))
print (credit_card)
   A  B  C  D  E
0  8  8  3  7  7
1  0  4  2  5  2
2  2  2  1  0  8
3  4  0  9  6  2
4  4  1  5  3  4

var1 = credit_card.var()
print (var1)
A     8.8
B    10.0
C    10.0
D     7.7
E     7.8
dtype: float64

var2 = credit_card.var(axis=1)
print (var2)
0     4.3
1     3.8
2     9.8
3    12.2
4     2.3
dtype: float64

如果需要具有 numpy.var 的numpy解决方案:

If need numpy solutions with numpy.var:

print (np.var(credit_card.values, axis=0))
[ 7.04  8.    8.    6.16  6.24]

print (np.var(credit_card.values, axis=1))
[ 3.44  3.04  7.84  9.76  1.84]

差异是因为默认情况下pandas中的ddof=1,但是您可以将其更改为0:

Differences are because by default ddof=1 in pandas, but you can change it to 0:

var1 = credit_card.var(ddof=0)
print (var1)
A    7.04
B    8.00
C    8.00
D    6.16
E    6.24
dtype: float64

var2 = credit_card.var(ddof=0, axis=1)
print (var2)
0    3.44
1    3.04
2    7.84
3    9.76
4    1.84
dtype: float64

这篇关于TypeError:"DataFrame"对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆