How to extract number with different alphabet data from pandas serie?
Question:
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
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")
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
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")