python - Looping over groups in a grouped dataframe -




consider 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 group "frame" with

grouped=frame.groupby(frame.index) 

now want loop on groups by:

for group in grouped: 

but next step not manage: how can extract group in each loop pandas data frame, can further process it?

df.groupby returns list of 2-tuples: index, , group. can iterate on each group this:

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

however, if want perform operation on groups, there better ways iteration.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -