简体   繁体   中英

Replace specific values in pandas dataframe with the corresponding column name, based on a condition,

My pandas dataframe has three possible entries:

  • 1 (integer)
  • NaN
  • arbitrary strings

An example could be created like that:

pd.DataFrame({'column1':['yes','some_string',np.NaN],
              'column2':[np.NaN,np.NaN,'yes']}, columns=['column1','column2'])

I want to achieve the following:

  1. Wherever the element value is yes I want it to be replaced with the column name column1 or column2 ;
  2. When the element is NaN, it should be untouched
  3. If there is a string some_string I want to put it into brackets, attach and and the corresponding column name: 'column1 and (some_string)'

I have tried a conditional df.apply but it did not work because it is applied on Series and I don't know how to impose a condition (=='yes')

I also tried pandas mask but I don't know how to replace with different elements rather than a static one.

IIUC try adding the brackets and 'and', then mask out the yes and radd the column names:

new_df = (' and (' + df + ')').mask(df.eq('yes'), '').radd(df.columns)

new_df :

                     column1  column2
0                    column1      NaN
1  column1 and (some_string)      NaN
2                        NaN  column2

Breakdown of steps:

new_df = ' and (' + df + ')'
              column1     column2
0           and (yes)         NaN
1   and (some_string)         NaN
2                 NaN   and (yes)

mask :

new_df = new_df.mask(df.eq('yes'), '')
              column1 column2
0                         NaN
1   and (some_string)     NaN
2                 NaN        

radd :

new_df = new_df.radd(df.columns)
                     column1  column2
0                    column1      NaN
1  column1 and (some_string)      NaN
2                        NaN  column2

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