Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping over groups in a grouped dataframe

Consider this small example:

data={"X":[1, 2, 3, 4, 5], "Y":[6, 7, 8, 9, 10], "Z": [11, 12, 13, 14, 15])
frame=pd.DataFrame(data,columns=["X","Y","Z"],index=["A","A","A","B","B"])

I want to group frame with

grouped=frame.groupby(frame.index)

Then I want to loop over the groups by:

for group in grouped:

But I'm stuck on the next step: How can I extract the group in each loop as a pandas DataFrame so I can further process it?

like image 401
Rockbar Avatar asked Aug 21 '17 12:08

Rockbar


People also ask

How do you iterate over a group in Python?

groupby() to Iterate over Data frame Groups. DataFrame. groupby() function in Python is used to split the data into groups based on some criteria.

Can you loop through a DataFrame?

DataFrame Looping (iteration) with a for statement. You can loop over a pandas dataframe, for each column row by row.

What does Group_by do in pandas?

What is the GroupBy function? Pandas' GroupBy is a powerful and versatile function in Python. It allows you to split your data into separate groups to perform computations for better analysis.


2 Answers

df.groupby returns an iterable of 2-tuples: the index, and the group. You can iterate over each group like this:

for _, g in frame.groupby(frame.index):
    .... # do something with `g`

However, if you want to perform some operation on the groups, there are probably better ways than iteration.

like image 192
cs95 Avatar answered Oct 19 '22 21:10

cs95


Here is an example:

groups = frame.groupby(level=0)

for n,g in groups:
    print('This is group '+ str(n)+'.')
    print(g)
    print('\n')

Output:

This is group A.
   X  Y   Z
A  1  6  11
A  2  7  12
A  3  8  13


This is group B.
   X   Y   Z
B  4   9  14
B  5  10  15
like image 45
Scott Boston Avatar answered Oct 19 '22 21:10

Scott Boston