Home > other >  Pandas dataframe aggregation with different operations
Pandas dataframe aggregation with different operations

Time:06-10

I have created a pandas dataframe called df with this code:

d = {'col1' : [5,3,2,1,34,54,6,7], 
        'col2' : [23,65,7,8,9,12,11,10], 
        'col3' : [65,67,7,11,7,7,9,10], 
        'col4' : [32,32,12,12,1,2,1,3],
        'ops' : [1,1,1,1,2,2,2,2]}

df = pd.DataFrame(data=d)
print(df)

The dataframe looks like this:

   col1  col2  col3  col4  ops
0     5    23    65    32    1
1     3    65    67    32    1
2     2     7     7    12    1
3     1     8    11    12    1
4    34     9     7     1    2
5    54    12     7     2    2
6     6    11     9     1    2
7     7    10    10     3    2

I need to group / aggregate the dataframe df by the column called ops so to get:

  • the first value of column col1
  • the minimum value of column col2
  • the maximum value of column col3
  • the last value of column col4

So, the resulting dataframe should look like:

enter image description here

Is there a pythonic way of doing it in one go?

CodePudding user response:

Let's try

out = df.groupby('ops', as_index=False).agg({'col1': 'first',
                                             'col2': 'min',
                                             'col3': 'max',
                                             'col4': 'last'})
print(out)

   ops  col1  col2  col3  col4
0    1     5     7    67    12
1    2    34     9    10     3
  • Related