Q:

What is best way to sum a column with different byte values? (M, G, K)

I have a dataset with the column, 'DiskWrite'. There are byte values such as M and G and K. I need to create a sum of this column which I later will create plots with. My concern is only with summing these values accurately. I have tried creating a separate column for units but have not got far.
**DiskWrite**
31.77M
6301.45M
1.50G

I later will group these values by month like so (random values created), dont need help with this, only need help with the sums above ^:
Start      Sum
10/19      12.54G
11/19      34.322G

Thanks in advance!

A:

you can convert all of them into one unit.I suggest the smallest one. in your question is Kb I guess. so check this:
from pandas import DataFrame
data = {"DiskWrite": ['31.77M', '6301.45M', '1.50G']}
df = DataFrame(data)
K, M, G = [], [], [] # to separate all units

for i in df['DiskWrite']:
    if i[-1] == 'K':
        K.append(float(i[:-1]))
    elif i[-1] == 'M':
        M.append(float(i[:-1]))
    elif i[-1] == 'G':
        G.append(float(i[:-1]))

for i in M: # to convert MegaBytes numbers to KiloBytes
    M[M.index(i)] = i * 1000
for i in G: # to convert GigaBytes numbers to KiloBytes
    G[G.index(i)] = i * 1000000

sum_ = sum(K + M + G) # now you have the sum of all numbers by KiloBytes unit.

>>> print(sum_)
#answer:
7833220.0

if you want the sum in Gigabytes, you can convert sum_ again by using sum_ / 1000000 or write code by changing two for loop for converting MegaBytes and KiloBytes to GigaBytes.

