DataFrame object is not callable error in Pandas
Often times when working with pandas in Jupyter, Pycharm, VSCode or other IDE, you might have encountered a the following TypeError exception:
TypeError: 'DataFrame' object is not callable
This error is easily fixed by using the brackets notation ( [ ] ) instead of round brackets( ( ) ) when creating a subset of a pandas DataFrame.
Understanding the error
Let’s create a very simple DataFrame from two Python lists:
import pandas as pd
team = ['West', 'East', 'North', 'South']
interviews = [120, 143, 78, 56]
# initializing the DataFrame
hr = pd.DataFrame(dict (team = team, interviews = interviews))
We can look at the DataFrame content by using the head() method:
hr.head()
Here’s our data:
team | interviews | |
---|---|---|
0 | West | 120 |
1 | East | 143 |
2 | North | 78 |
3 | South | 56 |
Now let’s assume that we want to sum the interviews column. We can easily reproduce our error by invoking the following snippet:
hr('interviews').sum()
This trigger the following exception:
Fixing the DataFrame object not callable error
The fix is pretty simple. Use the brackets notation instead of the round brackets to subset the interviews (or for practical purposes – any other column):
hr['interviews'].sum()
This will return the column sum – in our case 397.
Follow up learning
How o solve the import exception modulenotfound for the request library?