Skip to content Skip to sidebar Skip to footer

How To Retrieve Rows Of A Pandas Groupby Object In For Loop

I have a group by object.I want to retrieve rows of a particular column of the group by object in for loop & do some processing. For example,I'm giving here a sample code for g

Solution 1:

you can loop in the group_by object like this :

forindex, row in grouped:
    print (index) #index is a tuple print(row) #row is a new dataframe 

To check what you are looking for you can do this(ie: check if a value is in a certain column of a dataframe do this):

for index, rowin grouped:
    if -0.83026in row.get("C").values: # "C" oranycolumn name you want
        print("hello")

for your data the output will be something like this :

('bar', 'one')
     A    B        C         D
1  bar  one-0.830260.983017
('bar', 'three')
     A      B         C         D
3  bar  three -0.3810411.538971
('bar', 'two')
     A    B         C         D
5  bar  two -0.9634020.201348
('foo', 'one')
     A    B         C         D
0  foo  one0.6914100.3284206  foo  one-1.521541-0.188345
('foo', 'three')
     A      B         C         D
7  foo  three -0.817304-0.359331
('foo', 'two')
     A    B         C         D
2  foo  two -0.528639-0.9993014  foo  two -1.0189190.661665

Post a Comment for "How To Retrieve Rows Of A Pandas Groupby Object In For Loop"