Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Please I need help my csv file downloaded from this site

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

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

SyntaxError: invalid syntax

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

like image 577
H.Salam Avatar asked Dec 13 '22 18:12

H.Salam


1 Answers

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

df.columns

is not callable, you cannot df.columns() it, hence 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:

df['state'][0]

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

like image 80
TemporalWolf Avatar answered Dec 17 '22 22:12

TemporalWolf