1

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.

1
  • Please also let me know how to access the 'cities' variable as a two dimensional array or list if possible. Commented Mar 2, 2018 at 6:52

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

What you did was accidentally assigned a dictionary to the DataFrame attribute of the pd module. Then when you tried to create a data frame by calling the data frame constructor pd.DataFrame, python found a dictionary instead of the constructor. Whoops!
Yeah, coz before creating cities variable you have assigned a dictionary to pd.DataFrame. Restart your python repl and run your code again except don't run the line that I mentioned in my answer.
Or use import importlib; importlib.reload(pd)
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.
@piRSquared - Maybe the best is add this comments to answer, because very valuable, what do you think? Is possible edit Ankur answer?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.