0

I want to change a column from str to float. The column has now numbers with suffix 'M' or 'K' which I want to remove and keep only the number, and a string which I want to replace to 0.

1) In order to remove the chars M and K, I used it:

apps["Size"] = apps["Size"].map(lambda x: x.lstrip('M').rstrip('M'))
apps["Size"] = apps["Size"].map(lambda x: x.lstrip('K').rstrip('K'))

2) In order to change the value from "Varies with device" to 0, I tried using this:

mask = apps["Size"] == 'Varies with device'
apps.loc[mask, apps["Size"]] = '0'

What am I doing wrong? Is the any easier way to change it?

Thanks!

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
Eve
  • 1

1 Answers1

0

Remove units by Series.str.strip and then convert values to numeric with to_numeric and errors='coerce' for another non numeric values to missing values, which are replaced by Series.fillna:

apps["Size"] = pd.to_numeric(apps["Size"].str.strip('KM'), errors='coerce').fillna(0)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252