Pandas: Conditionally replace values based on other columns values

前端 未结 4 710
别那么骄傲
别那么骄傲 2021-02-06 11:52

I have a dataframe (df) that looks like this:

                    environment     event   
time                    
2017-04-28 13:08:22     NaN         add_rd  
         


        
4条回答
  •  抹茶落季
    2021-02-06 12:04

    Now my goal is for each add_rd in the event column, the associated NaN-value in the environment column should be replaced with a string RD.

    As per @Zero's comment, use pd.DataFrame.loc and Boolean indexing:

    df.loc[df['event'].eq('add_rd') & df['environment'].isnull(), 'environment'] = 'RD'
    

提交回复
热议问题