Mike.J
Mike.J

Reputation: 117

len() 'DataFrame' object is not callable

I am trying to get the length of the column if it is == 1.

Create Dictionary

 test = {'home': ['PIT', 'MIN', 'CHI', 'DET'], 'away': ['MIN', 'PIT', 'DET', 'CHI'], 'is_home': [1,1,1,1], 'home_win': [1,1,0,0],}

Create DF:

 new_test = pd.DataFrame(data=test)
 new_test # Display DataFrame

DF:

    home    away  is_home   home_win
0   PIT      MIN    1          1
1   MIN      PIT    1          1
2   CHI      DET    1          0
3   DET      CHI    1          0

Shape of DF to a string:

n_matches = new_test.shape[0]
n_matches
# 4

Number of home wins using new_test.home_win

n_homewins = len(new_test(new_test.home_win == 1))

Result:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-42-07ff7d4df92e> in <module>()
----> 1 n_homewins = len(new_test(new_test.home_win == int(1)))

TypeError: 'DataFrame' object is not callable

Upvotes: 0

Views: 661

Answers (2)

Matthieu Brucher
Matthieu Brucher

Reputation: 22023

num_testis a DataFrame, it is not callable:

n_homewins = len(new_test[new_test.home_win == 1])

Upvotes: 2

BernardL
BernardL

Reputation: 5464

You are passing your parameters in a parenthesis, that is why it tells you that the DataFrame is not callable. A small change should fix your problems:

n_homewins = len(new_test[new_test.home_win == 1])

We changed the parenthesis to square brackets for proper slicing.

Upvotes: 1

Related Questions