Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading