I have a dataframe like this:
df1 = pd.DataFrame({'col1' : ['cat', 'cat', 'dog', 'green', 'blue']})
and I want a new column that gives the category, like this:
dfoutput = pd.DataFrame({'col1' : ['cat', 'cat', 'dog', 'green', 'blue'],
'col2' : ['animal', 'animal', 'animal', 'color', 'color']})
I know I could do it inefficiently using .loc
:
df1.loc[df1['col1'] == 'cat','col2'] = 'animal'
df1.loc[df1['col1'] == 'dog','col2'] = 'animal'
How do I combine cat
and dog
to both be animal
? This doesn't work:
df1.loc[df1['col1'] == 'cat' | df1['col1'] == 'dog','col2'] = 'animal'