简体   繁体   中英

Data frame rows and columns replication from 1 frame to another

I want to group a specific column value x of column A of DF1 as a group name and aggregate the same row values present in column B as a series or a list

Then these series/list of values of column B replace DF2 Column A value x at first occurrence and duplicate the rows for the length of the values in series/list and add the values in the DF2 column A.

DF1:

A   B     
x   1  
x   2  
x   3       


DF2:
***
Red    Blue  Green  Yellow
x      10     car    four
x      11     Bus    six
y      12     bike   two
z      13     cycle  two 

I want the DF2 or a new DF to be formed as

DF3:

Red    Blue  Green  Yellow  
1      10     car    four
2      10     car    four
3      10     car    four
1      11     Bus    six
2      11     Bus    six
3      11     Bus    six
y      12     bike   two
z      13     cycle  two

Can someone help on the logic? preferably using pandas with less iterations.

You could do an outer merge and use bfill to combine the values in column A of df1 with column B :

df1.merge(df2, left_on ='A', right_on='Red', how='outer').bfill(axis=1)\
   .drop(['A','Red'], axis=1).rename(columns={'B':'Red'})

   Red Blue  Green Yellow
0   1   10    car   four
1   1   11    Bus    six
2   2   10    car   four
3   2   11    Bus    six
4   3   10    car   four
5   3   11    Bus    six
6   y   12   bike    two
7   z   13  cycle    two

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM