How to extract number with different alphabet data from pandas serie?

Question:

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

Asked By: mahi aslam

||

Answers:

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"{int(x[:-1])*1024}k")
Answered By: Zircoz
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.