Return multiple columns from pandas apply()

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

    It gives a new dataframe with two columns from the original one.

    import pandas as pd
    df = ...
    df_with_two_columns = df.apply(lambda row:pd.Series([row['column_1'], row['column_2']], index=['column_1', 'column_2']),axis = 1)
    

提交回复
热议问题