简体   繁体   中英

Pandas - group, sum and count

I have a dataframe that looks like this:

x = pd.DataFrame.from_dict({'row':[1, 1, 2, 3, 4,4,4], 'val_x': [1, 2, 3, 4, 5, 6, 7], 'i_x': [1, 2, 2,3, 5,5,6]})

   i_x  row  val_x
0    1    1      1
1    2    1      2
2    2    2      3
3    3    3      4
4    5    4      5
5    5    4      6
6    6    4      7

I want to group it by row, in each group sum up values of val_ix. and count different values of i_x. So the output should look like this:

   i_x  row  val_x
0    1    1      3
1    1    2      3
3    1    3      4
4    2    4      18

For example, if you look at the last row, it has i_x=2 because we have 2 different values in that group(5 and 6), and they add up to 5+6+7=18

Use agg

In [593]: x.groupby('row', as_index=False).agg({'i_x': 'nunique', 'val_x': 'sum'})
Out[593]:
   row  val_x  i_x
0    1      3    2
1    2      3    1
2    3      4    1
3    4     18    2

Same as

In [594]: x.groupby('row', as_index=False).agg({'i_x': pd.Series.nunique, 'val_x': np.sum})
Out[594]:
   row  val_x  i_x
0    1      3    2
1    2      3    1
2    3      4    1
3    4     18    2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM