Return multiple columns from pandas apply()

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

    Generally, to return multiple values, this is what I do

    def gimmeMultiple(group):
        x1 = 1
        x2 = 2
        return array([[1, 2]])
    def gimmeMultipleDf(group):
        x1 = 1
        x2 = 2
        return pd.DataFrame(array([[1,2]]), columns=['x1', 'x2'])
    df['size'].astype(int).apply(gimmeMultiple)
    df['size'].astype(int).apply(gimmeMultipleDf)
    

    Returning a dataframe definitively has its perks, but sometimes not required. You can look at what the apply() returns and play a bit with the functions ;)

提交回复
热议问题