Return multiple columns from pandas apply()

后端 未结 9 2112
灰色年华
灰色年华 2020-11-30 18:43

I have a pandas DataFrame, df_test. It contains a column \'size\' which represents size in bytes. I\'ve calculated KB, MB, and GB using the following code:

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 19:27

    Use apply and zip will 3 times fast than Series way.

    def sizes(s):    
        return locale.format("%.1f", s / 1024.0, grouping=True) + ' KB', \
            locale.format("%.1f", s / 1024.0 ** 2, grouping=True) + ' MB', \
            locale.format("%.1f", s / 1024.0 ** 3, grouping=True) + ' GB'
    df_test['size_kb'],  df_test['size_mb'], df_test['size_gb'] = zip(*df_test['size'].apply(sizes))
    

    Test result are:

    Separate df.apply(): 
    
        100 loops, best of 3: 1.43 ms per loop
    
    Return Series: 
    
        100 loops, best of 3: 2.61 ms per loop
    
    Return tuple:
    
        1000 loops, best of 3: 819 µs per loop
    

提交回复
热议问题