Home > Net >  How to execute a function on a group of rows in pandas dataframe and concatenate the results row wis
How to execute a function on a group of rows in pandas dataframe and concatenate the results row wis

Time:12-06

I have one DataFrame like below:

cell_id   col1   col2 

en_1  2.0   3.0
en_2  8.0   9.0
.
.
en_2  9.0   8.0  
en_1  9.0   8.0   
.
.
en_n  4.0   6.7

I want to send this DataFrame per cell_id once a the time to some function like below and concatenate the results on row wise(axis 0)

 def func(df):
     do_some_process
     return df

result1 = func(df[df.cell_id.eq('en_1')])
result2 = func(df[df.cell_id.eq('en_2')])
.
.
result_n = func(df[df.cell_id.eq('en_n')])

result = pd.concat([result1, result2,.....,result_n], axis=0)   

CodePudding user response:

You can simply use df.apply() as follows:

def func(x):
    #perform your operation on the pd.Series
    return x

df.apply(func, axis=1)

CodePudding user response:

If you need to some values depends on each row, simply you can use apply function and create a new column like this.

df['new_col'] = df.apply(func, axis=1)

Then if you want these values as row wise, you can assign reshaped version of that column to a variable.

t = df['new_col'].values.reshape(1, -1)

t will be a row version of that column if you need something like this.

CodePudding user response:

Example

data = {'cell_id': {0: 'en_1', 1: 'en_2', 2: 'en_2', 3: 'en_1', 4: 'en_3'},
        'col1': {0: 2.0, 1: 8.0, 2: 9.0, 3: 9.0, 4: 4.0},
        'col2': {0: 3.0, 1: 9.0, 2: 8.0, 3: 8.0, 4: 6.7}}
df = pd.DataFrame(data)

df

    cell_id col1 col2
0   en_1    2.0  3.0
1   en_2    8.0  9.0
2   en_2    9.0  8.0
3   en_1    9.0  8.0
4   en_3    4.0  6.7

Code

you can divide df by cell_id value

g = df.groupby('cell_id')
[g.get_group(i) for i in g.groups]

result:

[  cell_id  col1  col2
 0    en_1   2.0   3.0
 3    en_1   9.0   8.0,
   cell_id  col1  col2
 1    en_2   8.0   9.0
 2    en_2   9.0   8.0,
   cell_id  col1  col2
 4    en_3   4.0   6.7]

get list of dataframes



then you can apply your func and concat

pd.concat([func(g.get_group(i)) for i in g.groups])
  • Related