there are different unit for size : like k for 1,000, M for mega. I want to convert all data into same unit - bytes
may i know how to make it?
The expected result is update the size column into bytes like 9k will be 9,000
CodePudding user response:
def convert_unit(value):
if value in "kb":
#convert to bytes
return bytes
elif value in "mb":
# convert to bytes
return bytes
# the above function is just an example
df['column'].map(convert_unit)
You can map all the column values using the function. Redefine the function as per your need.
CodePudding user response:
i think this should work if your data is consitent:
def func(val):
# get the string suffix ('K'/'M')
val_char = val[-1:].lower()
if val_char == 'k':
return int(val[:-1]) * 1_000
elif val_char == 'm':
return int(val[:-1]) * 1_000_000
else:
return 0
df['size_bytes'] = df['size'].apply(lambda x: func(x))