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 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 consisting of rows and columns of a pandas DataFrame.
Practical Example: Understanding the error
We will create a very simple DataFrame from two Python lists and then use it to reproduce the TypeError exception:
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:
Here are our DataFrame rows:
Now let’s assume that we want to sum the interviews column. We can easily reproduce our error by invoking the following snippet:
This trigger the following exception:
Fixing the DataFrame object not callable error
The fix is pretty simple. We would like to find the sum of the interviews column. In order to subset that specific column we need to use the brackets notation instead of parentheses. Here’s is the correct snippet:
This will return the column sum – in our case 397.
Slicing specific DataFrame rows and avoid the TypeError
We can also slice only specific rows of our DataFrame. In this example we slice only the second and third rows of the interviews column. Here’s the snippet:
The result will be 221.