【问题标题】:Pandas dataframe: df.shape throws error 'TypeError: 'tuple' object is not callable'Pandas 数据框:df.shape 抛出错误'TypeError:'tuple'对象不可调用'
【发布时间】:2020-06-11 10:46:01
【问题描述】:

背景

我正在尝试获取数据框的形状。我使用 pd.read_csv 将数据从 csv 文件读取到数据帧,然后尝试获取其尺寸。

代码

file_name = 'xxxxxx.csv'

with open(file_name, 'r') as f:
    metadata_location = [i for i, x in enumerate(f.readlines()) if 'Metadata' in x]

with open(file_name, 'r') as f:
    data = pd.read_csv(f, index_col=False, skipfooter=26)
print(data.shape())

错误

TypeError: 'tuple' object is not callable

怎么解决??????

其他检查

print(type(data))
<class 'pandas.core.frame.DataFrame'>

【问题讨论】:

  • 直接调用data = pd.read_csv(file_name, index_col=False, skipfooter=26)不打开
  • 再次出现同样的错误 'TypeError: 'tuple' object is not callable'
  • 去掉形状后面的 ()。它是一个属性而不是类方法

标签: python pandas dimensions


【解决方案1】:

错误是因为shape()抛出错误,正确的方法是不带括号。

如果你改变:

print(data.shape())

为:

print(data.shape)

它将打印数据的形状

你真的需要第二个:用 open... 吗? Pandas 可以在没有 with open 行的情况下加载

【讨论】:

  • 我只是在回答之后看到了你的评论,对于@luigigi 的困惑感到抱歉,但我试过了,当你的评论尚未发布时,我在 jupyter 笔记本中看到了错误,至少在我的回答中加载页面。
猜你喜欢
  • 1970-01-01
  • 2021-10-14
  • 1970-01-01
  • 2019-10-05
  • 1970-01-01
  • 2021-08-19
  • 2019-08-09
  • 2018-12-03
  • 2012-04-21
相关资源
最近更新 更多