pandas TypeError: 'Index' object is not callable and SyntaxError: invalid syntax

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49075166/
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-09-14 05:15:32  来源:igfitidea点击:

TypeError: 'Index' object is not callable and SyntaxError: invalid syntax

pythonpython-3.xpandas

提问by H.Salam

Please I need help my csvfile downloaded from this site

请我需要帮助我csv从这个站点下载的文件

https://catalog.data.gov/dataset/state-drug-utilization-data-2016and I am working by pandas to analyses it I need to reach to column in the file and I do this

https://catalog.data.gov/dataset/state-drug-utilization-data-2016,我正在用大Pandas分析它我需要到达文件中的列,我这样做

import pandas as pd
url = "E:\dataset\state_dataset\Drug_Utilization_2017_-_California.csv"
df=pd.read_csv(url)
df.dropna(inplace=True)
df.shape
df.columns()

and the error was

错误是

TypeError: 'Index' object is not callable

类型错误:“索引”对象不可调用

when I try to Know the type of one column in the file I do this

当我尝试了解文件中一列的类型时,我会这样做

    type(df.'state'[0])

and "state" is an column in my csv file and the error was

“状态”是我的 csv 文件中的一列,错误是

SyntaxError: invalid syntax

语法错误:无效语法

Sorry to send two types of error but I am really try so many times and I fail.

很抱歉发送两种类型的错误,但我真的尝试了很多次但失败了。

回答by TemporalWolf

It may be helpful to follow a tutorial on how to use pandas:

遵循有关如何使用Pandas的教程可能会有所帮助:

df.columns

is not callable, you cannot df.columns()it, hence TypeError: 'Index' object is not callable.

不可调用,您不能调用df.columns(),因此TypeError: 'Index' object is not callable.

type(df.'state'[0])

is not how you get a column in pandas, they are not attributes of the dataframe and you can't use strings as attribute names, hence the SyntaxError:

不是在 Pandas 中获取列的方式,它们不是数据框的属性,您不能使用字符串作为属性名称,因此出现 SyntaxError:

df['state'][0]

is how you would get the first item in the 'state' column.

是如何获得“状态”列中的第一项。