r_31415
r_31415

Reputation: 8982

Unexpected output with pivot_tables in Pandas

I was following this blogpost when I found this behaviour in Pandas:

df = pd.read_csv("http://www.ats.ucla.edu/stat/data/binary.csv")
df.columns = ["admit", "gre", "gpa", "prestige"]
pd.pivot_table(df,values='admit',rows=['admit'], cols=['prestige'],aggfunc='count')
TypeError: 'DataFrame' object is not callable

but instead, if you refer to the rows and columns as df['admit'], df['prestige'], you get the expected result:

pd.pivot_table(df,values='admit',rows=df['admit'], cols=df['prestige'],aggfunc='count')
prestige   1   2   3   4
admit                   
0         28  97  93  55
1         33  54  28  12

According to the documentation, I should be able to refer to the DataFrame only using column's names in a list or array. Is this a bug or am I missing something?

Upvotes: 1

Views: 776

Answers (1)

Garrett
Garrett

Reputation: 49816

Here's a way to create that pivot table with 'size' as the aggregating function:

In [11]: df.pivot_table(rows='admit', cols='prestige', aggfunc='size')
Out[11]: 
prestige   1   2   3   4
admit                   
0         28  97  93  55
1         33  54  28  12

Upvotes: 1

Related Questions