jeudi 23 août 2018

Change variable based on other variable in Python [duplicate]

This question already has an answer here:

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