Typeerror: ‘dataframe’ object is not callable

When you are working with Pandas library one of the errors you can not avoid is Typeerror: ‘dataframe’ object is not callable.

Typically this error occurs when we use the parenthesis ( ) instead of square brackets [ ] in creating or accessing dataframe column.

In this article, we will explore how this error occurs, how to fix it, and how to prevent it from occurring in the future.

What is Typeerror: ‘dataframe’ object is not callable?

This error typically occurs in Python programming when you attempt to call a method or function on a DataFrame object, but mistakenly use parentheses ( ) instead of square brackets [ ].

Here is an example of a Dataframe of object as follows:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'Name': ['John', 'Emily', 'Charlie', 'David'],
        'Age': [25, 30, 20, 40],
        'Gender': ['Male', 'Female', 'Male', 'Male'],
        'Salary': [50000, 60000, 45000, 70000]})

#view DataFrame
print(df)

This will output the following:

      Name  Age  Gender  Salary
0     John   25    Male   50000
1    Emily   30  Female   60000
2  Charlie   20    Male   45000
3    David   40    Male   70000

How this ‘dataframe’ object is not callable occur

Given in the example above, if we want to display the values in Name column we should specify it in the print() function.

Here is the example code.

import pandas as pd

#create DataFrame
df = pd.DataFrame({'Name': ['John', 'Emily', 'Charlie', 'David'],
        'Age': [25, 30, 20, 40],
        'Gender': ['Male', 'Female', 'Male', 'Male'],
        'Salary': [50000, 60000, 45000, 70000]})

#view DataFrame
print( df('Name') )

If we run the code above, this will output an error since we wrongly used parenthesis in accessing the column.

Traceback (most recent call last):
  File "C:\Users\Windows\PycharmProjects\pythonProject1\main.py", line 10, in <module>
    print( df('Name') )
TypeError: 'DataFrame' object is not callable

Now let’s move to the next section where we are going to fix this error.

How to fix Typeerror: ‘dataframe’ object is not callable

One way to solve this error is just to replace the parenthesis ( ) with square brackets [ ] when accessing the column.

Here is an example, suppose we want to display the values in the Name column.

import pandas as pd

#create DataFrame
df = pd.DataFrame({'Name': ['John', 'Emily', 'Charlie', 'David'],
        'Age': [25, 30, 20, 40],
        'Gender': ['Male', 'Female', 'Male', 'Male'],
        'Salary': [50000, 60000, 45000, 70000]})

#view DataFrame
print( df['Name'] )

Here is the output:

0       John
1      Emily
2    Charlie
3      David
Name: Name, dtype: object

Use the dot (.) notation

Alternatively, we can also use the dot (.) notation instead of brackets while accessing the dataframe column.

import pandas as pd

#create DataFrame
df = pd.DataFrame({'Name': ['John', 'Emily', 'Charlie', 'David'],
        'Age': [25, 30, 20, 40],
        'Gender': ['Male', 'Female', 'Male', 'Male'],
        'Salary': [50000, 60000, 45000, 70000]})

#view DataFrame
print( df.Salary)

Output:

0    50000
1    60000
2    45000
3    70000
Name: Salary, dtype: int64

Tips to Avoid this error

To avoid the “TypeError: ‘DataFrame’ object is not callable” error in Python, you can follow these tips:

  1. Use square brackets [ ] to access elements in a DataFrame, rather than parentheses ().
  2. Check for any typos or syntax errors in your code, as they can lead to this error.
  3. Make sure you are using the correct variable names and data types when calling functions or methods on your DataFrame.
  4. Avoid using the same variable name for both a DataFrame object and a function, as this can cause confusion and lead to this error.
  5. Use descriptive variable names to help prevent naming conflicts and make your code more readable.

FAQs

What causes the “TypeError: ‘DataFrame’ object is not callable” error?

The error occurs when you try to call a DataFrame object as if it were a function, usually by using parentheses () instead of square brackets [ ] to access a specific element in the DataFrame.

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

To fix the error, make sure you are using square brackets [ ] to access elements in the DataFrame, check for any syntax errors or typos in your code.

Also, use the correct variable names and data types, and avoid using the same variable name for both a DataFrame object and a function.

Conclusion

In conclusion, the TypeError: ‘DataFrame’ object is not callable error in Python occurs when a DataFrame object is called as if it were a function. Usually by using parentheses ( ) instead of square brackets [ ] to access a specific element in the DataFrame.

It is important to be consistent and careful when working with DataFrames to avoid this error and ensure that your code runs smoothly.

We hope that this guide has helped you resolve this error and get back to coding.

If you are finding solutions to some errors you might encounter we also have  TypeError: not supported between instances of ‘str’ and ‘int’

Thank you for reading!