How can I fix the TypeError: ‘DataFrame’ object is not callable error?

How can I fix the TypeError: ‘DataFrame’ object is not callable error?

The TypeError: ‘DataFrame’ object is not callable error occurs when trying to call a function on a DataFrame object in Python. This error can be fixed by ensuring that the function is being called correctly, with the appropriate syntax and parameters. Additionally, double-checking the variable names and data types being used can help identify any potential issues. If the error persists, it may be necessary to consult the documentation for the specific function or seek assistance from a more experienced programmer.

Fix: TypeError: ‘DataFrame’ object is not callable


One common error you may encounter when using pandas is:

TypeError: 'DataFrame' object is not callable

This error usually occurs when you attempt to perform some calculation on a variable in a pandas DataFrame by using round () brackets instead of square [ ] brackets.

The following example shows how to use this syntax in practice.

How to Reproduce the Error

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
print(df)

  team  points  assists  rebounds
0    A      18        5        11
1    B      22        7         8
2    C      19        7        10
3    D      14        9         6
4    E      14       12         6
5    F      11        9         5
6    G      20        9         9
7    H      28        4        12

Now suppose we attempt to calculate the mean value in the “points” column:

#attempt to calculate mean value in points column
df('points').mean()

TypeError: 'DataFrame' object is not callable

Since we used round () brackets, pandas thinks that we’re attempting to call the DataFrame as a function.

Since the DataFrame is not a function, we receive an error.

How to Fix the Error

The way to resolve this error is to simply use square [ ] brackets when accessing the points column instead round () brackets:

#calculate mean value in points column
df['points'].mean()

18.25

We’re able to calculate the mean of the points column (18.25) without receiving any error since we used squared brackets.

Also note that we could use the following dot notation to calculate the mean of the points column as well:

#calculate mean value in points column
df.points.mean()

18.25

Additional Resources

The following tutorials explain how to fix other common errors in Python:

Cite this article

stats writer (2024). How can I fix the TypeError: ‘DataFrame’ object is not callable error?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-fix-the-typeerror-dataframe-object-is-not-callable-error/

stats writer. "How can I fix the TypeError: ‘DataFrame’ object is not callable error?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-fix-the-typeerror-dataframe-object-is-not-callable-error/.

stats writer. "How can I fix the TypeError: ‘DataFrame’ object is not callable error?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-fix-the-typeerror-dataframe-object-is-not-callable-error/.

stats writer (2024) 'How can I fix the TypeError: ‘DataFrame’ object is not callable error?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-fix-the-typeerror-dataframe-object-is-not-callable-error/.

[1] stats writer, "How can I fix the TypeError: ‘DataFrame’ object is not callable error?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I fix the TypeError: ‘DataFrame’ object is not callable error?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top