比较列并替换现有列中的结果

时间:2018-07-26 10:50:54

标签: python-3.x pandas dataframe

我有两列pandas列,我首先比较两列,然后用新的替换旧的字符串。

我的数据:

shopping  on_List
Banana    1 
Apple     0
Grapes    1 
None      0
Banana    1
Nuts      0
Lemon     1

为了比较两者,我做了以下工作:

results = []
for shopping, on_list in zip(df.shopping, df.on_list):
    if shopping != 'None' and on_list == 1:
        items = shopping

    if items == 'Banana':
        re = items.replace('Banana', 'Bananas')
    elif items == 'Lemon':
        re = items.replace('Lemon', 'Lemons')
    elif items == 'Apples':
        re= items.replace('Apple','Apples')

results.append(re)
print(results) 
Output: ['Bananas','Lemons', 'Apples']

理想情况下,我想返回一个新列,用“购物”列中的旧值替换新值:

这是我想要的输出,但是不幸的是,我的新列表(结果)与当前df的长度不同:

shopping 
Bananas
Apples 
Grapes 
None 
Bananas 
Nuts 
Lemons

2 个答案:

答案 0 :(得分:2)

我建议创建用于映射的字典并替换过滤后的值:

d = {'Banana':'Bananas', 'Lemon':'Lemons', 'Apple':'Apples'}

mask = df['on_List'].eq(1) & df['on_List'].notnull()
df['shopping'] = df['shopping'].mask(mask, df['shopping'].map(d)).fillna(df['shopping'])
#slowier solution
#df['shopping'] = df['shopping'].mask(mask, df['shopping'].replace(d))
print (df)
  shopping  on_List
0  Bananas        1
1    Apple        0
2   Grapes        1
3     None        0
4  Bananas        1
5     Nuts        0
6   Lemons        1

答案 1 :(得分:0)

val = []
for i in range(len(df)):
    if df["shopping"][i] != None and df["on_List"][i] == 1:
        if df["shopping"][i] == "Banana":
            val.append("Bananas")
        elif df["shopping"][i] == "Lemon":
            val.append("Lemons")
        elif df["shopping"][i] == "Apple":
            val.append("Apples")
    else:
        val.append("None")

df["Result"] = pd.Series(val)