Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'DataFrame' object is not callable

I've programmed these for calculating Variance

import pandas as pd
import xlrd
import numpy as np
import matplotlib.pyplot as plt


credit_card=pd.read_csv("default_of_credit_card_clients_Data.csv",skiprows=1)

print(credit_card.head())
for col in credit_card:
    var[col]=np.var(credit_card(col))

print(var)

I'm getting this error

Traceback (most recent call last):
   File "C:/Python34/project.py", line 11, in <module>
     var[col]=np.var(credit_card(col)) TypeError: 'DataFrame' object is not callable
like image 323
Amar Shivaram Avatar asked Feb 01 '17 06:02

Amar Shivaram


People also ask

What does it mean by series object not callable?

The Python "TypeError: 'Series' object is not callable" occurs when we try to call a Series object as if it were a function. To solve the error, resolve any clashes between function and variable names and don't override built-in functions.

What is TypeError in pandas?

Pandas TypeError. Pandas throws this error because it can not find a positional argument it was expecting in your function. A positional argument will not have a default value and is required for your function to run. You will run into this error when you are running a pandas script or jupyter notebook cell block.

How do you convert PySpark DF to pandas DF?

Convert PySpark Dataframe to Pandas DataFramePySpark DataFrame provides a method toPandas() to convert it to Python Pandas DataFrame. toPandas() results in the collection of all records in the PySpark DataFrame to the driver program and should be done only on a small subset of the data.

What is non callable in Python?

The Python "TypeError: object is not callable" occurs when we try to call a not-callable object (e.g. a list or dict) as a function using parenthesis () . To solve the error, make sure to use square brackets when accessing a list at index or a dictionary's key, e.g. my_list[0] .


1 Answers

It seems you need DataFrame.var:

Normalized by N-1 by default. This can be changed using the ddof argument

var1 = credit_card.var()

Sample:

#random dataframe
np.random.seed(100)
credit_card = pd.DataFrame(np.random.randint(10, size=(5,5)), columns=list('ABCDE'))
print (credit_card)
   A  B  C  D  E
0  8  8  3  7  7
1  0  4  2  5  2
2  2  2  1  0  8
3  4  0  9  6  2
4  4  1  5  3  4

var1 = credit_card.var()
print (var1)
A     8.8
B    10.0
C    10.0
D     7.7
E     7.8
dtype: float64

var2 = credit_card.var(axis=1)
print (var2)
0     4.3
1     3.8
2     9.8
3    12.2
4     2.3
dtype: float64

If need numpy solutions with numpy.var:

print (np.var(credit_card.values, axis=0))
[ 7.04  8.    8.    6.16  6.24]

print (np.var(credit_card.values, axis=1))
[ 3.44  3.04  7.84  9.76  1.84]

Differences are because by default ddof=1 in pandas, but you can change it to 0:

var1 = credit_card.var(ddof=0)
print (var1)
A    7.04
B    8.00
C    8.00
D    6.16
E    6.24
dtype: float64

var2 = credit_card.var(ddof=0, axis=1)
print (var2)
0    3.44
1    3.04
2    7.84
3    9.76
4    1.84
dtype: float64
like image 112
jezrael Avatar answered Oct 12 '22 17:10

jezrael