Home > other >  Automatically merging a large number of dataframes
Automatically merging a large number of dataframes

Time:10-22

I had a big dataframe that looks like this:

df=
team     workplace    job    employee
a        w1           j1     x
a        w1           j1     y
a        w1           j2     z
b        w1           j1     x
b        w1           j3     t
c        w2           j4     s
c        w2           j5     u

I split it into multiple dataframes based on team:

grouper = [g[1] for g in df.groupby(['team'])

I also have a second dataframe that looks like this:

df2=
employee    workplace    feedback    question
x           w1           True        q1
x           w1           False       q2
x           w1           True        q2
y           w1           True        q1
y           w1           False       q4
z           w1           False       q1
z           w1           False       q2
z           w1           True        q2
t           w1           False       q3
s           w2           True        q6
s           w2           False       q6
u           w2           True        q6
u           w2           False       q7

I know how to add the questions and feedback to a dataframe with teams:

grouper0_feedbacks = grouper[0].merge(df2, on=['employee', 'workplace']) 

But how can I do it for all of them without writing it manually, when there are so many dataframes in grouper?

CodePudding user response:

Change order of operations should working here - first merge, then split:

df1 = df.merge(df2, on=['employee', 'workplace']) 

grouper0_feedbacks = [g for _, g in df1.groupby(['team'])]

If really need it use list comprehension for loop:

grouper = [g for _, g in df.groupby(['team'])]
grouper0_feedbacks = [g.merge(df2, on=['employee', 'workplace']) for g in grouper]
  • Related