如何比较列并添加新列

时间:2019-02-11 06:51:48

标签: python-3.x pandas

fruit      color  country
Apple      red    india
Apple      green  china 
banana     yellow  Aus  
banana     green   USA      
grapes     black   China  
grapes     green   korea
grapes     red     japan
grapes     white   USA
grapes     yellow  Aus
Orange     orange  INDIa
Orange     green   India

如果两个记录的水果列相等,则检查国家列,如果任何列具有INDIA然后打印IPL,不包含印度则打印NON -IPL,如果所有都是印度,则打印ALL-IPL IN NEXT NEW列类别。 / p>

必需的输出是

fruit       category
Apple        IPL
banana       Non-IPL
grapes       Non-IPL
Orange       All-IPL  

1 个答案:

答案 0 :(得分:1)

lower将列转换为小写,按Series.eq比较,然后合计all any,最后使用numpy.select来设置类别:

df1 = df['country'].str.lower().eq('india').groupby(df['fruit']).agg(['any','all'])

masks = [df1['all'], df1['any'] & ~df1['all']]
vals = ['All-IPL','IPL']

cats = np.select(masks, vals, default='Non-IPL')
print (cats)
['IPL' 'All-IPL' 'Non-IPL' 'Non-IPL']

df2 = pd.DataFrame({'Fruit': df1.index, 'Category':cats})
print (df2)
    Fruit Category
0   Apple      IPL
1  Orange  All-IPL
2  banana  Non-IPL
3  grapes  Non-IPL