Mapping column value based on another column in python

孤街浪徒 提交于 2019-12-24 21:07:07

问题


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!


回答1:


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 NaNs values:

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


来源:https://stackoverflow.com/questions/50561577/mapping-column-value-based-on-another-column-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!