pythonpandas

pandas apply multiple columns


Starting from this dataframe

df = pd.DataFrame(
    np.arange(3*4).reshape((4, 3)),
    index=['a', 'b', 'c', 'd'],
    columns=['A', 'B', 'C']
)
print(df)
   A   B   C
a  0   1   2
b  3   4   5
c  6   7   8
d  9  10  11

I want to apply two functions to each column to generate two columns for each original column to obtain this shape, with a multiindex column nested below each original column:

    A        B        C     
    x    y   x    y   x    y
a  10  100  11  101  12  102
b  13  103  14  104  15  105
c  16  106  17  107  18  108
d  19  109  20  110  21  111

however, something like this doesn't work

df.apply(lambda series:
    series.transform([lambda x: x+10, lambda x: x+100])
)

and raises ValueError: If using all scalar values, you must pass an index

Note that I do not want to use agg like in this answer, since this is not an aggregation. I also want to avoid referring to column names directly.


Solution

  • You just need to use df.transform() and give your functions names.

    def x(k):
        return k + 10
    
    def y(k):
        return k + 100
    
    df.transform([x, y])
    
        A        B        C     
        x    y   x    y   x    y
    a  10  100  11  101  12  102
    b  13  103  14  104  15  105
    c  16  106  17  107  18  108
    d  19  109  20  110  21  111