Home > Back-end >  How to extract number with different alphabet data from pandas serie?
How to extract number with different alphabet data from pandas serie?

Time:12-18

dataset

I have a dataset that contains a column called size. In that column, I have sizes in Megabytes and Kilobytes, like this, 19M, 853k. How can I extract the number and multiply Mb with nothing and divided kb by 1024 to get each value of the column or vice-versa? Or making it all bytes will do too.

The result set should be the only number. So, first, extract then multiply or division then change the data type

CodePudding user response:

Assuming your column is called size. You can do the following in pandas:

df["size in K"] = df["size"].apply(lambda x: x if s[-1]=='k' else f"{x*1024}k")
  • Related