lundi 29 mars 2021

Remove a string from certain column values and then operate them Pandas

I have a dataframe with a column named months (as bellow), but it contains some vales passed as "x years". So I want to remove the word "years" and multiplicate them for 12 so all column is consistent.

index  months
1        5
2        7
3        3 years
3        9 
4        10 years

I tried with

if df['months'].str.contains("years")==True:
  
   df['df'].str.rstrip('years').astype(float) * 12

But it's not working

Aucun commentaire:

Enregistrer un commentaire