This question already has an answer here:
- Conditional Replace Pandas 3 answers
- How to implement 'in' and 'not in' for Pandas dataframe 5 answers
I have a dataframe called df containing a variable called Category. I would like to create a new variable that takes different "names" depending on whether words exist in the first variable. I am trying something like this:
df['Investment_type'] = 'Mixed'
if 'Stocks' in df.Category:
df.Investment_type = 'Stocks'
if 'Bonds' in df.Category:
df.Investment_type = 'Bonds'
A value of df.Category could e.g. be "Foreign Stocks". I get no error, but a variable that only takes the value 'Mixed'.
/Jonas
Aucun commentaire:
Enregistrer un commentaire