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'
Build your dict
then do map
d={'dog':'ani','cat':'ani','green':'color','blue':'color'}
df1['col2']=df1.col1.map(d)
df1
col1 col2
0 cat ani
1 cat ani
2 dog ani
3 green color
4 blue color
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With