Python 类型错误:“DataFrame”对象不可调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41973423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:00:02  来源:igfitidea点击:

TypeError: 'DataFrame' object is not callable

pythonpandasnumpymatplotlib

提问by Amar Shivaram

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)

I'm getting this error

我收到这个错误

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

回溯(最近一次调用):文件“C:/Python34/project.py”,第 11 行,在 var[col]=np.var(credit_card(col)) TypeError: 'DataFrame' object is not callable

A solution will be appreciated.

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

回答by jezrael

It seems you need DataFrame.var:

看来你需要DataFrame.var

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

默认按 N-1 归一化。这可以使用 ddof 参数进行更改

var1 = credit_card.var()

Sample:

样本:

#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

If need numpy solutions with numpy.var:

如果需要 numpy 解决方案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]

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

差异是因为默认情况下ddof=1在 中pandas,但您可以将其更改为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