熊猫:基于两个列值创建一个新列

时间:2018-11-21 04:52:06

标签: python pandas dataframe

学习列比较。 如何基于两列创建一个新列?

我可以做两个条件的水果或蔬菜。但是对于第三个条件,这是无法做到的。 :(

df
    basket1     basket2
0   fruit       fruit
1   vegetable   vegetable 
2   vegetable   both
3   fruit       both

结果

Newdf

    basket1    basket2    total
0   fruit      fruit      fruit
1   vegetable  vegetable  vegetable  
2   vegetable  both       Unknown
3   fruit      both      fruit

非常感谢您的帮助!

4 个答案:

答案 0 :(得分:2)

创建自己的函数并使用DataFrame.apply

In [104]: def total(r):
     ...:     if r.a == r.b:
     ...:         return r.a
     ...:     elif (r.a == 'fruit') and (r.b == 'both'):
     ...:         return r.a
     ...:     return 'Unknown'
     ...:

In [105]: df = pd.DataFrame({'a': ['fruit', 'vegetable', 'vegetable', 'fruit'], 'b': ['fruit', 'vegetable', 'both', 'both']})

In [106]: df
Out[106]:
           a          b
0      fruit      fruit
1  vegetable  vegetable
2  vegetable       both
3      fruit       both

In [107]: df['total'] = df.apply(total, axis=1)

In [108]: df
Out[108]:
           a          b      total
0      fruit      fruit      fruit
1  vegetable  vegetable  vegetable
2  vegetable       both    Unknown
3      fruit       both      fruit

答案 1 :(得分:2)

df["total"] = df.apply(lambda x: x.a if (x.a == x.b) or ((x.a == 'fruit') and (x.b == 'both')) else 'Unkonwn', axis = 1)

输出

           a          b      total
0      fruit      fruit      fruit
1  vegetable  vegetable  vegetable
2  vegetable       both    Unkonwn
3      fruit       both      fruit

答案 2 :(得分:1)

这里使用np.select

的解决方案
df['total'] = np.select([df['a']==df['b'], (df['a']=='fruit')&(df['b']=='both')], [df['a'], 'fruit'], 'Unkown')

输出:

    a          b           total
0   fruit      fruit       fruit
1   vegetable  vegetable   vegetable
2   vegetable  both        Unknown 
3   fruit      both        fruit

答案 3 :(得分:0)

  

data.csv

basket1,basket2
fruit,fruit
vegetable,vegetable
vegetable,both
fruit,both
  

code.py

import pandas as pd

df = pd.read_csv('data.csv')
for i, r in df.iterrows():
    if df.at[i, 'basket1'] == df.at[i, 'basket2']:
        df.at[i, 'total'] = df.at[i, 'basket1']
    else:
        df.at[i, 'total'] = 'something else'

输出:

     basket1    basket2           total
0      fruit      fruit           fruit
1  vegetable  vegetable       vegetable
2  vegetable       both  something else
3      fruit       both  something else