I have a dataframe(df):
A B C D E F
A1 B1 c 10 E1 F1
A2 B2 c 50 E2 F2
A3 B3 c 20 E2 F3
A4 B4 c 30 E1 F4
A5 B5 c 40 E3 F5
Output should be like;
df:
A B C D E F count sum
A1 B1 c 10 E1 F1 2 40
A2 B2 c 50 E2 F2 2 70
A3 B3 c 20 E2 F3 2 70
A4 B4 c 30 E1 F4 2 40
A5 B5 c 40 E3 F5 1 40
taking count and sum based on E column.
I tried :
df.groupby('E').agg(['count','sum'])
I am getting only count and sum column not other columns of df
. Please suggest the solution.
Try it like this:
df['count'] = df.groupby('E')['E'].transform('count')
df['sum'] = df.groupby('E')['D'].transform('sum')
Output:
A B C D E F count sum
0 A1 B1 c 10 E1 F1 2 40
1 A2 B2 c 50 E2 F2 2 70
2 A3 B3 c 20 E2 F3 2 70
3 A4 B4 c 30 E1 F4 2 40
4 A5 B5 c 40 E3 F5 1 40
我无法理解您的数据,但您想要做的是:
df.groupby('E').agg({'column1': 'count', 'column2': 'sum'})
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.