Reputation: 53
I have a list such as
groups = [['Group1', 'A', 'B'], ['Group2', 'C', 'D']]
and a dataframe such as
A 100
B 200
C 300
D 400
I want to make a group sum from the list above to become:
Group 1 300
Group 2 700
How can I do this using python pandas? Needless to say I am a newbie in pandas. Thanks.
Upvotes: 5
Views: 1942
Reputation: 862511
You need create dict
by lists
and then groupby
and aggregating sum
:
df = pd.DataFrame({'a': ['A', 'B', 'C', 'D'], 'b': [100, 200, 300, 400]})
print (df)
a b
0 A 100
1 B 200
2 C 300
3 D 400
groups = [['Group1', 'A', 'B'], ['Group2', 'C', 'D']]
#http://stackoverflow.com/q/43227103/2901002
d = {k:row[0] for row in groups for k in row[1:]}
print (d)
{'B': 'Group1', 'C': 'Group2', 'D': 'Group2', 'A': 'Group1'}
print (df.set_index('a').groupby(d).sum())
b
Group1 300
Group2 700
Is possible a bit modify solution - if where only column b
is aggregate by sum
. Last reset_index
for convert index to column.
df1 = df.set_index('a').groupby(pd.Series(d, name='a'))['b'].sum().reset_index()
print (df1)
a b
0 Group1 300
1 Group2 700
df2 = df.groupby(df['a'].map(d))['b'].sum().reset_index()
print (df2)
a b
0 Group1 300
1 Group2 700
Upvotes: 4
Reputation: 294218
Use python 3 unpacking and comprehension to create dictionary. Use that dictionary in a map on first column. Use that mapping to group by.
Consider the list of lists groups
and dataframe df
groups = [['Group1', 'A', 'B'], ['Group2', 'C', 'D']]
df = pd.DataFrame(dict(a=list('ABCD'), b=range(100, 401, 100)))
Then:
df.groupby(df.a.map({k: g for g, *c in groups for k in c})).sum()
b
a
Group1 300
Group2 700
Upvotes: 1
Reputation: 7306
Another option...but seems @jezrael's way is better!
import pandas as pd
groups = [['Group1', 'A', 'B'], ['Group2', 'C', 'D']]
df0 = pd.melt(pd.DataFrame(groups).set_index(0).T)
df1 = pd.read_clipboard(header=None) # Your example data
df = df1.merge(df0, left_on=0, right_on='value')[['0_y', 1]]
df.columns = ['Group', 'Value']
print df.groupby('Group').sum()
Value
Group
Group1 300
Group2 700
Upvotes: 1