Home > Mobile >  Groupby pandas data-frame based on values from another df
Groupby pandas data-frame based on values from another df

Time:11-05

I have a data-frame in which I keep all my relevant attributes and another one which has attributes based on which I want to group the first df.

I know you can groupby the data-frame if you put a series(one column) as an argument, by if you put a data-frame you get an error.

ValueError: Grouper for '<class 'pandas.core.frame.DataFrame'>' not 1-dimensional

I know I could just concat the columns to the original data-frame, but I would prefer not to, unless there is no other solution.

df.groupby([sorted_team_names]).ngroup()

This is my code. sorted_team_names is a df with two columns, furthermore it has the same index as df.

It is a rather general question, I am not sure you need a data-sample.

CodePudding user response:

One way is groups by columns separately:

df1 = pd.DataFrame({'a':[1,2,2,1], 'b':[1,2,2,1]})
print (df1)
   a  b
0  1  1
1  2  2
2  2  2
3  1  1

df2 = pd.DataFrame({'c':[1,2,3,7]})
print (df2)
   c
0  1
1  2
2  3
3  7

df3 = df2.groupby([df1['a'], df1['b']]).sum()
print (df3)
     c
a b   
1 1  8
2 2  5

...but indices have to match between both DataFrames:

df1 = pd.DataFrame({'a':[1,2,2,1], 'b':[1,2,2,1]}, index=[2,5,6,8])
print (df1)
   a  b
2  1  1 <- matched only 2 index
5  2  2
6  2  2
8  1  1

df2 = pd.DataFrame({'c':[1,2,3,7]})
print (df2)
   c
0  1
1  2
2  3
3  7

df3 = df2.groupby([df1['a'], df1['b']]).sum()
print (df3)
         c
a   b     
1.0 1.0  3
  • Related