I am trying to group the average of columns B and C by column A . I am unsure how to accomplish this using the groupby function:
import pandas as pd
pd.DataFrame({'B' : df.groupby(['A'])['B'].mean()}).reset_index()
Because I can only figure out how to find the average and group a single column by another. If this is my Current Dataframe :
A B C
0 70 12
0 20 14
0 46 11
1 25 9
2 86 2
2 14 6
This is my Expected Outcome . Grouping all like values of A together, corresponding values in B and C are averaged in their respective columns:
A B C
0 45.3 12.3
1 25 9
2 50 4
Try this
df.groupby('A').mean()
Out[339]:
B C
A
0 45.333333 12.333333
1 25.000000 9.000000
2 50.000000 4.000000
Although .mean()
is the most obvious solution, you can also aggregate the results:
>>> df.groupby('A').agg('mean')
B C
A
0 45.333333 12.333333
1 25.000000 9.000000
2 50.000000 4.000000
Using .agg
, you can aggregate multiple functions simultaneously, eg:
>>> df.groupby('A').agg(['mean', 'std'])
B C
mean std mean std
A
a 45.333333 25.006666 12.333333 1.527525
b 25.000000 NaN 9.000000 NaN
c 50.000000 50.911688 4.000000 2.828427
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.