# Python：按条件求和

``````df = pd.DataFrame({'condition' : ['a','b','b','b','a','a'],
'name' : ['one', 'one', 'two', 'three', 'three', 'three'],
'data1' : [7, 3, 48, 13, 27, 12]})
df
condtion  data1   name
0   a          7    one
1   b          3    one
2   b         48    two
3   b         13    three
4   a         27    three
5   a         12    three
``````

``````df1
name   total
0   one      7
1   two     48
2   three   39
``````

``````df = df.groupby(['name','condition'], sort=False)['data1'].sum().unstack()
df['total'] = df['a'].fillna(df['b'])
print (df)
condition     a     b  total
name
one         7.0   3.0    7.0
two         NaN  48.0   48.0
three      39.0  13.0   39.0
``````

``````df1 = df.reset_index().rename_axis(None, 1)[['name','total']]
print (df1)
name  total
0    one    7.0
1    two   48.0
2  three   39.0
``````

``````def f(x):
if (x['condition'] == 'a').any():
return x.loc[x['condition'] == 'a', 'data1'].sum()
else:
return x.loc[x['condition'] == 'b', 'data1'].sum()

df1 = df.groupby('name', sort=False).apply(f).reset_index(name='total')
print (df1)
name  total
0    one      7
1    two     48
2  three     39
``````

``````a = df.loc[df['condition'] == 'a'].groupby('name', sort=False)['data1'].sum()
b = df.loc[df['condition'] == 'b'].groupby('name', sort=False)['data1'].sum()

df = a.combine_first(b).reset_index(name='total')
print (df)
name  total
0    one    7.0
1  three   39.0
2    two   48.0
``````

0 条评论