【发布时间】:2019-05-30 14:30:49
【问题描述】:
我有一个这样的数据框:
df1 = pd.DataFrame({'col1' : ['cat', 'cat', 'dog', 'green', 'blue']})
我想要一个提供类别的新列,如下所示:
dfoutput = pd.DataFrame({'col1' : ['cat', 'cat', 'dog', 'green', 'blue'],
'col2' : ['animal', 'animal', 'animal', 'color', 'color']})
我知道我可以使用.loc 低效地做到这一点:
df1.loc[df1['col1'] == 'cat','col2'] = 'animal'
df1.loc[df1['col1'] == 'dog','col2'] = 'animal'
如何将cat 和dog 组合成animal?这不起作用:
df1.loc[df1['col1'] == 'cat' | df1['col1'] == 'dog','col2'] = 'animal'
【问题讨论】:
标签: python pandas dictionary series