简体   繁体   中英

Mapping column value based on another column in python

I am trying to change column value depends from the other column along its row then merge it to an existing excel file using the ADSL column as my key.

I have a Data like this:

ADSL     Status     Result
2/134    WO         No Server Answer
1/239    WO         Faulty
2/94     FA         Number
2/321    SP         Voltage

This is an actual data, the Status column has three possible value [WO, FA, SP] each value has equivalent Result value.

example:

Status                  Equivalent Result Value

                        Battery Tone
                        Engage
  WO                    No Dial Tone
                        No Server Answer
                        No Voltage
                        Number

  SP                    Voltage

  FA                    Faulty
                        Vacant

now in reality the Status column is not getting the right value based on its equivalent Result value. (see data above)

What I'm trying to do is to correct the status value based on its equivalent value from Result column

what is the easiest or efficient way to do this in python? I am not looking on a particular library tho. any help would be appreciated a lot. Cheers!

I believe need map by Series :

df2['Status'] = df2['Status'].map(df1.set_index('Result')['Status'])

If some values are not match is possible replace by original non NaN s values:

df2['Status'] = df2['Status'].map(df1.set_index('Result')['Status']).fillna(df2['Status'])

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