12

I've programmed these for calculating Variance

credit_card = pd.read_csv("default_of_credit_card_clients_Data.csv", skiprows=1)
    
for col in credit_card:
    var[col]=np.var(credit_card(col))

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
0

3 Answers 3

10

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
0
1

This error occurs when you call a pandas DataFrame object - use round () brackets - as if it were a class or a function. Long story short, pandas DataFrames are objects of type 'DataFrame' whose attribute that makes an object callable is null.

For example, in the OP, the culprit is:

credit_card(col)

because previously, credit_card was defined to be a pandas DataFrame object via

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

The most common way to define a dataframe is via the constructor df = pd.DataFrame(data) and this df cannot be called either: df() would give the same error. The list of other functions that construct a dataframe object can be found in the pandas documentation.


This problem is essentially a typo. Here are some common causes/solutions:

  1. Perhaps you were trying to select some columns of the dataframe, in which case, use the square [] brackets:
    credit_card[col]                # e.g. credit_card['colA'] or credit_card[['colA']]
    
  2. Perhaps you were trying to call a dataframe method, in which case, write the method's name after the dataframe's name, separated by a dot .:
    credit_card.some_method(col)    # e.g. credit_card.value_counts('colA')
    
  3. Perhaps you have a function and a dataframe sharing the same name in your environment. For example,
    credit_card = lambda x: x + 1
    credit_card = pd.DataFrame(data)
    
    In that case, make sure to re-define them using different names.
0

I got the same error when mistaking boolean indexing.

self_views = views[views('author_id') == views('viewer_id')]

the correct syntax is

self_views = views[views['author_id'] == views['viewer_id']]

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.