Skip to main content
deleted 351 characters in body
Source Link
Pyd
  • 6.2k
  • 19
  • 59
  • 117

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 

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)].reshape(len(df),2),index=df.index,columns=['g1/column1', 'g1/column2'])) print(new_df)   name  g1/column1 g1/column2 #0 AAAA  10.0  20.0 #1 AAAA  30.0  40.0 

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.drop(col2,axis=1) print(df) # name g1/column1  g1/column2 #0 AAAA 10.0 20.0 #1 AAAA 30.0 40.0 
added 306 characters in body
Source Link
Pyd
  • 6.2k
  • 19
  • 59
  • 117

you need np.flattendf.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)].reshape(len(df),2),index=df.index,columns=['g1/column1', 'g1/column2'])) print(new_df) name g1/column1 g1/column2 #0 AAAA 10.0 20.0 #1 AAAA 30.0 40.0 

you need np.flatten

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)].reshape(len(df),2),index=df.index,columns=['g1/column1', 'g1/column2'])) print(new_df) name g1/column1 g1/column2 #0 AAAA 10.0 20.0 #1 AAAA 30.0 40.0 

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)].reshape(len(df),2),index=df.index,columns=['g1/column1', 'g1/column2'])) print(new_df) name g1/column1 g1/column2 #0 AAAA 10.0 20.0 #1 AAAA 30.0 40.0 
Source Link
Pyd
  • 6.2k
  • 19
  • 59
  • 117

you need np.flatten

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)].reshape(len(df),2),index=df.index,columns=['g1/column1', 'g1/column2'])) print(new_df) name g1/column1 g1/column2 #0 AAAA 10.0 20.0 #1 AAAA 30.0 40.0