如果column等于特定值,请在新列中插入新值-Python

时间:2019-04-10 05:45:33

标签: python pandas where

我正在尝试创建一个新的column,其中包含基于单独column中的值的值。具体来说,对于下面的df,我想在['Val'] == 'A','B','C'中插入'X'到其中的值。与['New_Val']类似,但插入一个'D','E','F'

'Y'

预期输出:

import pandas as pd

d = ({
    'Val' : ['A','B','C','D','E','F'],                                     
     })

df = pd.DataFrame(data = d)

Xs = ['A','B','C']
Ys = ['D','E','F']

df['New_Val'] = df.loc['New_Val'].loc[df['Val'] == Xs] = 'X'
df['New_Val'] = df.loc['New_Val'].loc[df['Val'] == Ys] = 'Y'

1 个答案:

答案 0 :(得分:1)

按字典使用Series.map

#specify values
d = {'X':Xs, 'Y':Ys}
print (d)
{'X': ['A', 'B', 'C'], 'Y': ['D', 'E', 'F']}

#swap key values in dict of lists
#http://stackoverflow.com/a/31674731/2901002
d1 = {k: oldk for oldk, oldv in d.items() for k in oldv}
print (d1)
{'A': 'X', 'B': 'X', 'C': 'X', 'D': 'Y', 'E': 'Y', 'F': 'Y'}


df['New_Val'] = df['Val'].map(d1)
print (df)
  Val New_Val
0   A       X
1   B       X
2   C       X
3   D       Y
4   E       Y
5   F       Y