Shadin
Shadin

Reputation: 1967

Pandas - df.size() error: 'numpy.int64' object is not callable

Using pandas, I'm trying to plot some data using:

df.size().unstack().plot(kind=barh)

but I got this error:

TypeError: 'numpy.int64' object is not callable

then I tried df.size() only and got the same error. Now I'm not sure what causes this because according to the documentation, DataFrame.size() should work fine. Any idea?

Upvotes: 7

Views: 6987

Answers (1)

jezrael
jezrael

Reputation: 862396

There is one problem you need omit () in DataFrame.size, but output is scalar, so impossible call unstack:

df.size

Sample:

df = pd.DataFrame({'A':list('abcdef'),
                   'B':[4,5,4,5,5,4],
                   'C':[7,8,9,4,2,3],
                   'D':[1,3,5,7,1,0],
                   'E':[5,3,6,9,2,4],
                   'F':list('aaabbb')})

print (df)
   A  B  C  D  E  F
0  a  4  7  1  5  a
1  b  5  8  3  3  a
2  c  4  9  5  6  a
3  d  5  4  7  9  b
4  e  5  2  1  2  b
5  f  4  3  0  4  b

a = df.size
print (a)
36

Maybe need groupby + GroupBy.size():

df1 = df.groupby(['F', 'B']).size().unstack()
print (df1)
B  4  5
F      
a  2  1
b  1  2

Upvotes: 5

Related Questions