Last updated: Apr 10, 2024
Reading timeยท4 min
The Python "TypeError: 'DataFrame' object is not callable" error occurs when
we use parentheses ()
instead of square brackets []
to access a column in a
DataFrame
.
To solve the error, make sure to use square brackets when accessing the column.
Assume you have the following DataFrame object.
import pandas as pd data = { 'id': [1, 2, 3], 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [100, 200, 300], } df = pd.DataFrame(data) print(df)
The code sample produces the following output.
id name salary 0 1 Alice 100 1 2 Bobby 200 2 3 Carl 300
If you try to access a specific column using parentheses ()
, you'd get an
error.
import pandas as pd data = { 'id': [1, 2, 3], 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [100, 200, 300], } df = pd.DataFrame(data) # โ๏ธ TypeError: 'DataFrame' object is not callable df('name') # ๐๏ธ Using parentheses ()
We used parentheses ()
instead of square brackets []
when accessing the
column which caused the error.
Instead, use square brackets []
to select a subset of a DataFrame
.
import pandas as pd data = { 'id': [1, 2, 3], 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [100, 200, 300], } df = pd.DataFrame(data) print(df['name']) # โ Using square brackets []
Notice that we used square brackets []
when accessing the name
column.
The code sample produces the following output.
0 Alice 1 Bobby 2 Carl Name: name, dtype: object
If you need to call a method with the values in a column, use square brackets
[]
when accessing the column and parentheses ()
to invoke the method.
import pandas as pd data = { 'id': [1, 2, 3], 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [100, 200, 300], } df = pd.DataFrame(data) print(df['salary'].mean()) # ๐๏ธ 200.0
We used square brackets []
to access the salary
column and then called the
mean()
method using parentheses.
DataFrame
are accessed using square brackets []
.Functions and methods are invoked using parentheses ()
.
If you need to get a subset of a DataFrame
object, use square brackets.
import pandas as pd data = { 'id': [1, 2, 3], 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [100, 200, 300], } df = pd.DataFrame(data) print(df[0:2])
The code sample returns the first 2 rows of the DataFrame
object.
id name salary 0 1 Alice 100 1 2 Bobby 200
You can also access a specific column after the slicing operation.
import pandas as pd data = { 'id': [1, 2, 3], 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [100, 200, 300], } df = pd.DataFrame(data) print(df[0:2]['name'])
The code sample produces the following output.
0 Alice 1 Bobby Name: name, dtype: object
We only selected the first and second rows of the name
column in the
DataFrame
.
If you need to call a method with a subset of the rows of a column, use parentheses.
import pandas as pd data = { 'id': [1, 2, 3], 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [100, 200, 300], } df = pd.DataFrame(data) print(df[0:2]['salary'].mean()) # ๐๏ธ 150.0
We selected the first 2 rows of the salary
column and then called the mean()
method on the values.
If you need to calculate the variance over an axis, use the pandas.DataFrame.var() method.
import pandas as pd data = { 'id': [1, 2, 3], 'name': ['Alice', 'Bobby', 'Carl'], 'height': [1.61, 1.87, 1.49] } df = pd.DataFrame(data) print(df.var())
The code sample produces the following output.
id 1.000000 height 0.037733 dtype: float64
By default, the ddof
(Delta Degrees of Freedom) argument is set to N - ddof
where N is the number of elements.
You can set ddof
to 0
to normalize by N
instead of N - 1
.
import pandas as pd data = { 'id': [1, 2, 3], 'name': ['Alice', 'Bobby', 'Carl'], 'height': [1.61, 1.87, 1.49] } df = pd.DataFrame(data) # id 0.666667 # height 0.025156 # dtype: float64 print(df.var(ddof=0))
The code sample produces the following output.
id 0.666667 height 0.025156 dtype: float64
DataFrame
objectAnother common cause of the error is having a function with the same name as the
variable that stores your DataFrame
object.
import pandas as pd data = { 'id': [1, 2, 3], 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [100, 200, 300], } def df(): return 'bobbyhadz.com' df = pd.DataFrame(data) # โ๏ธ TypeError: 'DataFrame' object is not callable print(df())
We initially named a function df
and then declared a df
variable, setting it
to a DataFrame
object.
When we try to call the df
variable, we actually end up trying to call the
DataFrame
object which causes the error.
To solve the error, rename the function or the DataFrame
object so that the
names don't clash.
import pandas as pd data = { 'id': [1, 2, 3], 'name': ['Alice', 'Bobby', 'Carl'], 'salary': [100, 200, 300], } def df(): return 'bobbyhadz.com' # ๐๏ธ Renamed variable my_df = pd.DataFrame(data) print(df()) # ๐๏ธ bobbyhadz.com
I renamed the variable, so it doesn't clash with the function anymore.
The df()
line now calls the function as expected.
You can learn more about the related topics by checking out the following tutorials: