0

I have the following (pandas) DataFrame:

    b    c    d    e
0  100  369  203  314
1  100  228  784  366
2  200  811  664  202
3  200  531  932  575

I want to iterate its rows but I need to know where the value of b changed. I am looking for a way to get this DF content in groups or something like that and then iterate over the rows of each group (with a nested loop and in this way I can get an indication that the b value did changed):

In the first iteration my new df will be:

    b    c    d    e
0  100  369  203  314
1  100  228  784  366

In the second iteration my new df will be:

    b    c    d    e
0  200  811  664  202
1  200  531  932  575
Andy Thomas
  • 1,219
  • 3
  • 19
  • 33

1 Answers1

0

You can use the groupby method:

from pandas import DataFrame
columns = ["b", "c", "d", "e"]
data = [[100, 369, 203, 314], [100, 228, 784, 366], [200, 811, 664, 202], [200, 531, 932, 575]]
df = DataFrame(data=data,columns=columns)

def split_dfs(df, col):
    return [group[1] for group in list(df.groupby(col))]

dfs = split_dfs(df, "b")

for df_group in dfs:
    print(df_group)
    b    c    d    e
0  100  369  203  314
1  100  228  784  366
     b    c    d    e
2  200  811  664  202
3  200  531  932  575
birdmw
  • 865
  • 10
  • 18