How to Fix the TypeError: ‘DataFrame’ object is not callable in Python (2 Examples)

 

This article demonstrates how to debug the “TypeError: ‘DataFrame’ object is not callable” in the Python programming language.

Table of contents:

You’re here for the answer, so let’s get straight to the programming part!

 

Example Data & Libraries

We first have to import the pandas library:

import pandas as pd                              # Load pandas

As a next step, we also need to create some data that we can use in the exemplifying code later on.

data = pd.DataFrame({'x1':range(70, 64, - 1),    # Create pandas DataFrame
                     'x2':['a', 'b', 'c', 'a', 'b', 'c'],
                     'x3':[1, 7, 5, 9, 1, 5]})
print(data)                                      # Print pandas DataFrame
#    x1 x2  x3
# 0  70  a   1
# 1  69  b   7
# 2  68  c   5
# 3  67  a   9
# 4  66  b   1
# 5  65  c   5

 

Example 1: Reproduce the TypeError: ‘DataFrame’ object is not callable

In Example 1, I’ll explain how to replicate the “TypeError: ‘DataFrame’ object is not callable” in the Python programming language.

Let’s assume that we want to calculate the variance of the column x3. Then, we might try to use the Python code below:

data('x3').var()                                 # Code leads to error
# TypeError: 'DataFrame' object is not callable

Unfortunately, the Python console returns the error message “TypeError: ‘DataFrame’ object is not callable” after executing the previous Python syntax.

The reason for this is that we have tried to use round parentheses to select the variable x3 (i.e. data(‘x3’)).

Let’s solve this problem!

 

Example 2: Debug the TypeError: ‘DataFrame’ object is not callable

In Example 2, I’ll show how to fix the “TypeError: ‘DataFrame’ object is not callable”.

To achieve this, we have to use square brackets instead of round parentheses to extract our pandas DataFrame column (i.e. data[‘x3’]).

Consider the syntax below:

data['x3'].var()                                 # Code works fine
# Out[13]: 10.266666666666666

The previous Python code has returned a proper result, i.e. the variance of the column x3 is equal to 10.266666666666666. No error messages are returned anymore.

 

Video, Further Resources & Summary

Do you need more explanations on the Python codes of this tutorial? Then you may want to watch the following video on my YouTube channel. In the video, I’m explaining the Python programming code of this article in a live session:

 

 

Furthermore, you may read the related articles on my website. A selection of articles is listed here:

 

At this point you should have learned how to handle the “TypeError: ‘DataFrame’ object is not callable” in the Python programming language. Let me know in the comments, in case you have additional questions. In addition, don’t forget to subscribe to my email newsletter to receive updates on new tutorials.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


2 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top