简体   繁体   中英

Adding a new column to the pandas dataframe by merging other columns and renaming based on previous column names

I want to merge several columns into a new one and rename the values based on those column names. I have this dataframe:

    P1  P2  P3  P4
1               T1
2       T4
3           T3
4   T1
5           T6

And want to change it to this one (adding the last column):

    P1  P2  P3  P4  PAll
1               T1  P4
2       T4          P2
3           T3      P3
4   T1              P1
5           T6      P3

I have tried + and also stacking but could not reach the final answer.

I appreciate your help. Thanks

Using pandas.DataFrame.eq and idxmax :

df['PALL'] = df.eq('T1').idxmax(1)
print(df)

Output:

   P1  P2  P3  P4 PALL
0              T1   P4
1      T1           P2
2          T1       P3
3  T1               P1
4          T1       P3

If need all values, not only first use DataFrame.dot :

df['PAll'] = df.notna().dot(df.columns + ';').str.rstrip(';')
print (df)
    P1   P2   P3   P4 PAll
1  NaN  NaN  NaN   T1   P4
2  NaN   T4  NaN  NaN   P2
3  NaN  NaN   T3  NaN   P3
4   T1  NaN  NaN  NaN   P1
5  NaN  NaN   T6  NaN   P3

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