Tampa
Tampa

Reputation: 78264

Pandas group by will not work

what am i missing here? I am trying to do a group by.

asp = np.array(np.array([0,0,1]))
asq = np.array(np.array([10,10,20]))
columns=['asp']
df = pd.DataFrame(asp, index=None, columns=columns)
df['asq'] = asq
print df
df.groupby(by=['asp']).sum()
print df
   asp  asq
0    0   10
1    0   10
2    1   20
   asp  asq
0    0   10
1    0   10
2    1   20

results should be:

    asp  asq
0    0   20
1    1   20

Upvotes: 6

Views: 21658

Answers (1)

DSM
DSM

Reputation: 353079

df.groupby doesn't change df; it returns a new object. In this case you perform an aggregation operation, so you get a new DataFrame. You have to give a name to the result if you want to use it later:

>>> df_summed = df.groupby('asp').sum()
>>> df_summed
     asq
asp     
0     20
1     20

[2 rows x 1 columns]

Upvotes: 11

Related Questions