Return multiple columns from pandas apply()

后端 未结 9 2113
灰色年华
灰色年华 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:22

    Some of the current replies work fine, but I want to offer another, maybe more "pandifyed" option. This works for me with the current pandas 0.23 (not sure if it will work in previous versions):

    import pandas as pd
    
    df_test = pd.DataFrame([
      {'dir': '/Users/uname1', 'size': 994933},
      {'dir': '/Users/uname2', 'size': 109338711},
    ])
    
    def sizes(s):
      a = locale.format("%.1f", s['size'] / 1024.0, grouping=True) + ' KB'
      b = locale.format("%.1f", s['size'] / 1024.0 ** 2, grouping=True) + ' MB'
      c = locale.format("%.1f", s['size'] / 1024.0 ** 3, grouping=True) + ' GB'
      return a, b, c
    
    df_test[['size_kb', 'size_mb', 'size_gb']] = df_test.apply(sizes, axis=1, result_type="expand")
    

    Notice that the trick is on the result_type parameter of apply, that will expand its result into a DataFrame that can be directly assign to new/old columns.

提交回复
热议问题