you need df.combine_first,
col1=['g1/column1', 'g1/column2'] col2=['g1/g2/column1', 'g2/column2'] df[col1]=df[col1].combine_first(pd.DataFrame(df[col2].values,columns=col1)) df=df[['name']+col1] print(df) # name g1/column1 g1/column2 #0 AAAA 10.0 20.0 #1 AAAA 30.0 40.0 another way,
cols=['g1/column1', 'g1/column2', 'g1/g2/column1', 'g2/column2'] arr=df[cols].values.flatten() new_df=df[['name']].join(pd.DataFrame(arr[~np.isnan(arr)]df=df.reshape(lendrop(df),2),index=df.index,columns=['g1/column1'col2, 'g1/column2'])axis=1) print(new_dfdf) # name g1/column1 g1/column2 #0 AAAA 10.0 20.0 #1 AAAA 30.0 40.0