Return multiple columns from pandas apply()

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

    Really cool answers! Thanks Jesse and jaumebonet! Just some observation in regards to:

    • zip(* ...
    • ... result_type="expand")

    Although expand is kind of more elegant (pandifyed), zip is at least **2x faster. On this simple example bellow, I got 4x faster.

    import pandas as pd
    
    dat = [ [i, 10*i] for i in range(1000)]
    
    df = pd.DataFrame(dat, columns = ["a","b"])
    
    def add_and_sub(row):
        add = row["a"] + row["b"]
        sub = row["a"] - row["b"]
        return add, sub
    
    df[["add", "sub"]] = df.apply(add_and_sub, axis=1, result_type="expand")
    # versus
    df["add"], df["sub"] = zip(*df.apply(add_and_sub, axis=1))
    

提交回复
热议问题