pandas: expand and replace rows with rows from another data frames

Question:

I have two data frames with many different column names but a few common ones. One frame has rows that have to be "expanded" with rows from the other data frame:

Example:

df = pd.DataFrame({'option':['A', 'A', 'B', 'B', 'fill_A', 'fill_B', ], 'items':['11111', '22222', '33333', '11111', '', '', ], 'other_colA':['','', '','', '','' ]})

look_up_df = pd.DataFrame({'option':['A','A','A','B', 'B','B'], 'items':['11111', '22222', '33333', '44444', '55555', '66666'],  'other_colB':['','', '','', '','' ]})
df

Data Frame to fill

Rows "fill_A" and "fill_B" in df have to be replace with rows from look_up_df like so:

enter image description here

How do this expansion while leaving out of the rest of columns ?

Asked By: euh

||

Answers:

You can use boolean indexing, a merge and concat:

m = df['option'].str.startswith('fill_')

pd.concat([df[~m],
           df.loc[m, 'option'].str.replace('fill_', '').to_frame()
           .merge(look_up_df, on='option')
          ])

Output:

  option  items other_colA other_colB
0      A  11111                   NaN
1      A  22222                   NaN
2      B  33333                   NaN
3      B  11111                   NaN
0      A  11111        NaN           
1      A  22222        NaN           
2      A  33333        NaN           
3      B  44444        NaN           
4      B  55555        NaN           
5      B  66666        NaN           
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.