Home > Enterprise >  How to convert to MB or KB the pandas describe table?
How to convert to MB or KB the pandas describe table?

Time:08-20

df.describe()

enter image description here

I have a network dataset. To be able to understand the dataset better, I want to find a way to convert the numbers to MB or KB for packets and bytes.

df.head()

enter image description here

CodePudding user response:

You could just divide the column by 1024 to get it in KB or 1024*1024 to get it in MB.

cols = ['TotBytes', 'SrcBytes', 'DstBytes']

for c in cols:
    df[c] = df[c] / 1024
    # In case you need to round then do..
    # df[c] = (df[c] / 1024).round(0) 

There is some confusion as to whether KB is 1000 or 1024 so you can divide by 1000 too if you wish.


Update

If in addition you want to make it easier to read by not showing the scientific e notation, then you could follow this answer:

df.describe().apply(lambda s: s.apply('{0:.2f}'.format))
  • Related