Skip to content
Advertisement

Slice Dataframe in sub-dataframes when specific string in column is found

Assume I have the dataframe df and I want to slice this in multiple dataframes and store each in a list (list_of_dfs).

Each sub-dataframe should only contain the rows “Result”. One sub-dataframe starts, when in column “Point” the value “P1” and in column “X_Y” the value “X” is given.

I tried this with first finding the indicies of each “P1” and then slicing the overall dataframe within a list comprehension using the indicies of “P1”. But I receive a list with two empty dataframes. Can someone advise? Thanks!

JavaScript

Advertisement

Answer

JavaScript
  • get the subset of the frame where Step is equal to “Result”
  • check in which rows there is “P1” and “X” sequence
    • that gives a True/False series
    • cumulative sum of it determines the group as the “pivoting” (turning) points will be True since False == 0 in numeric context
    • iterating over a GroupBy object yields “group_label, sub_frame” pairs, out of which we pull the sub_frames

to get

JavaScript

where the intermediares were

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement