简体   繁体   中英

Convert number with units in python to NON human readable format

What would be the best way to convert a numerical column containing float AND unit as in :

df = pd.DataFrame(["211.301 MB","435.5 GB","345.234 Bytes"])

expected output in Bytes for example:

211.301*1024*1024 = 221565157.376

Many questions like this one : Reusable library to get human readable version of file size?

are showing ways of doing the opposite : convert number to human readable. How to convert human readable to float ?

Is there a more efficient way than splitting :

spl = pd.DataFrame(dataf['Total_Image_File_Size'].str.split(' ',expand=True))

and then parsing the units column with multiples if's ?

Thanx

I think this one should work: https://pypi.python.org/pypi/humanfriendly

>>> import humanfriendly
>>> user_input = raw_input("Enter a readable file size: ")
Enter a readable file size: 16G
>>> num_bytes = humanfriendly.parse_size(user_input)
>>> print num_bytes
17179869184
>>> print "You entered:", humanfriendly.format_size(num_bytes)
You entered: 16 GB

You could create function to convert text to value and use apply

import pandas as pd

df = pd.DataFrame(["211.301 MB","435.5 GB","345.234 Bytes"])


def convert(text):

    parts = text.split(' ')

    value = float(parts[0])

    if parts[1] == 'KB':
        value *= 1024
    elif parts[1] == 'MB':
        value *= 1024 * 1024
    elif parts[1] == 'GB':
        value *= 1024 * 1024

    return value



df['value'] = df[0].apply(convert)


           0         value  
0     211.301 MB  2.215652e+08  
1       435.5 GB  4.566548e+08  
2  345.234 Bytes  3.452340e+02

EDIT: you could use humanfriendly in this function instead of if/elif

Just another idea.

>>> for size in "211.301 MB", "435.5 GB", "345.234 Bytes":
        number, unit = size.split()
        print float(number) * 1024**'BKMGT'.index(unit[0])

221565157.376
4.67614564352e+11
345.234

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM