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
Post a Comment