jeudi 16 novembre 2017

Python Data frame: Replace Value in Column If 2 Separate Conditions are Met

Logic I'm trying to implement:

  1. If 'phasename' column contains C1, C2, C4, or C18 AND 'reqmnt' column contains C
  2. Replace C in requirement column with value in 'phasename' column
  3. OTHERWISE leave 'reqmnt' column as is

Below is a sample screen shot of what I'm trying to do enter image description here

Currently, I'm not able to figure out how to incorporate the AND condition in my python code. So I'm getting the results from current_column in the screen shot.

# create mapping dictionary 
    phasename_dictionary = {'C1':'C1', 'C2':'C2', 'C4':'C4', 'C18':'C18'}
    # replace column value 
    df.loc[df['phasename'].isin(phasename_dictionary.keys()), 'reqmnt'] = df['phasename'].map(phasename_dictionary)

Aucun commentaire:

Enregistrer un commentaire