Dance Party
Dance Party

Reputation: 3713

How to iterate over groups

I am trying to iterate over groups (produced by group.by in Pandas) in order to apply a function (create a chart in MatPlotLib) and get a result for each group in the DataFrame. I thought to do something like this, but I know there's a better/functional way:

import pandas as pd
DF = pd.DataFrame({'COL1': ['A', 'A','B','B'], 
                   'COL2' : [1,1,2,2],
                   'COL3' : [2,3,4,5]})


unique_list = set(df['COL1'])
for group in unique_list:
    if group == df['COL1']:
       <apply function to df['COL3'] 
       one group at a time, 
       starting with df['COL1'] == 'A'> 

Upvotes: 0

Views: 251

Answers (1)

Shahram
Shahram

Reputation: 814

The result of the groupby function is a pandas data frame or series. You can use the apply function. See below example:

groupedDF = DF.groupby(['COL1']).sum()

#Let's assume we want apply below function
def square(x):
    return x['COL2']*x['COL2']

#Below line will return the results with applied function
groupedDF.apply(square,axis=1)

#Let's assume Returned value is series
groupedSeries = DF.groupby(['COL1'])['COL2'].sum()

#Let's assume we want apply below function
def square(x):
    return x*x

#Below line will return the result
groupedSeries.apply(square)

Upvotes: 1

Related Questions