Vikranth Inti
Vikranth Inti

Reputation: 135

"TypeError: 'dict' object is not callable" in Python pandas DataFrame

I imported pandas as pd.

>>>import pandas as pd

Assigned Series variables 'city_names' and 'population'

>>> city_names=pd.Series(['San Fransisco','San Jose','Sacromento'])
>>> population=pd.Series([8964,91598,034892])

Created DataFrame

>>> pd.DataFrame=({'City Name': city_names, 'Population':population})

While assigning DataFrame to the variable 'cities',

>>> cities=pd.DataFrame({'City Name ' : city_names, 'Population' : population})

I am getting the following error :

Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    cities=pd.DataFrame({'City Name ' : city_names, 'Population' : population})
TypeError: 'dict' object is not callable

Also aware that 'dict' is a dictionary with (key,value) pairs! please let me know why this error occurs. I looked into other questions as well but could'nt find help.

Upvotes: 1

Views: 8253

Answers (1)

Ankur
Ankur

Reputation: 33637

Your code pd.DataFrame=({'City Name': city_names, 'Population':population}) replaced pd.DataFrame function with a dictionary.


@Ankur please modify this edit to your liking.
PiR edit:

pd.DataFrame is a class defined by pandas. A class is "callable" meaning that you can call it with parentheses like so pd.DataFrame(). However, in Python the = is an assignment operator and you did pd.DataFrame = {} which assigned some dictionary to the same spot that the DataFrame class constructor used to be. You should not use that line specified by @Ankur. It will break your stuff every time.

Upvotes: 3

Related Questions