moys
moys

Reputation: 8033

How to assign the multiple values of an output to new multiple columns of a dataframe?

I have the following function:

def sum(x):
    oneS = x.iloc[0:len(x)//10].agg('sum')
    twoS = x.iloc[len(x)//10:2*len(x)//10].agg('sum')
    threeS = x.iloc[2*len(x)//10:3*len(x)//10].agg('sum')
    fourS = x.iloc[3*len(x)//10:4*len(x)//10].agg('sum')
    fiveS = x.iloc[4*len(x)//10:5*len(x)//10].agg('sum')
    sixS = x.iloc[5*len(x)//10:6*len(x)//10].agg('sum')
    sevenS = x.iloc[6*len(x)//10:7*len(x)//10].agg('sum')
    eightS = x.iloc[7*len(x)//10:8*len(x)//10].agg('sum')
    nineS = x.iloc[8*len(x)//10:9*len(x)//10].agg('sum')
    tenS = x.iloc[9*len(x)//10:len(x)//10].agg('sum')
    return [oneS,twoS,threeS,fourS,fiveS,sixS,sevenS,eightS,nineS,tenS]

How to assign the outputs of this function to columns of dataframe (which already exists)

The dataframe I am applying the function is as below

Cycle   Type    Time
1   1   101
1   1   102
1   1   103
1   1   104
1   1   105
1   1   106
9   1   101
9   1   102
9   1   103
9   1   104
9   1   105
9   1   106

The dataframe I want to add the columns is something like below & the new columns Ones, TwoS..... Should be added like shown & filled with the results of the function.

Cycle   Type    OneS    TwoS    ThreeS
1   1           
9   1           
8   1           
10  1           
3   1           
5   2           
6   2           
7   2           

If I write a function for just one value and apply it like the following, it is possible:

grouped_data['fm']= data_train_bel1800.groupby(['Cycle', 'Type'])['Time'].apply( lambda x: fm(x))

But I want to do it all at once so that it is neat and clear.

Upvotes: 1

Views: 40

Answers (1)

jezrael
jezrael

Reputation: 862791

You can use:

def f(x):
    out = []
    for i in range(10):
        out.append(x.iloc[i*len(x)//10:(i+1)*len(x)//10].agg('sum'))
    return pd.Series(out)


df1 = (data_train_bel1800.groupby(['Cycle', 'Type'])['Time']
                         .apply(f)
                         .unstack()
                         .add_prefix('new_')
                         .reset_index())

print (df1)
   Cycle  Type  new_0  new_1  new_2  new_3  new_4  new_5  new_6  new_7  new_8  \
0      1     1      0    101    102    205    207    209    315    211    211   
1      9     1      0    101    102    205    207    209    315    211    211   

   new_9  
0    106  
1    106  

Upvotes: 2

Related Questions