基于python中的另一列映射列值 [英] Mapping column value based on another column in python

查看:90
本文介绍了基于python中的另一列映射列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改列值取决于其行中的另一列,然后使用 ADSL 列作为我的键将其合并到现有的 excel 文件中.

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.

我有这样的数据:

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

这是一个实际数据,Status 列有三个可能的值 [WO, FA, SP] 每个值都有等价的 Result 值.

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

示例:

Status                  Equivalent Result Value

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

<小时>

  SP                    Voltage

<小时>

  FA                    Faulty
                        Vacant

现在实际上 Status 列没有根据其等效的 Result 值获得正确的值.(见上面的数据)

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

我想要做的是根据 Result 列中的等效值更正 status

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

在python中最简单或有效的方法是什么?我不是在看一个特定的图书馆.任何帮助将不胜感激.干杯!

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!

推荐答案

我相信需要 map by Series:

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

如果某些值不匹配,可以替换为原始的非 NaN 值:

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'])

这篇关于基于python中的另一列映射列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆