mardi 25 août 2020

manipulate a column of dataframe with conditions

enter code hereIn order to change strings' suffix to be prefix in a column of dataframe, which is made with the following code for example.

import pandas as pd
df = pd.DataFrame({'a':['100000.ss','200000.zz'],'b':[10,18]},index=[1,2])


      a         b
1   100000.ss   10
2   200000.zz   18

I tried one line code below, but the result shows the if else statement doesn't work. Why?

df['a'] = df['a'].apply(lambda x: 'ss.'+x[:6] if x.find("ss") else 'zz.'+x[:6])


      a         b
1   ss.100000   10
2   ss.200000   18

Aucun commentaire:

Enregistrer un commentaire