Skip to content Skip to sidebar Skip to footer

How To Split A Dataframe Each Time A String Value Changes In A Column?

I've got a dataframe of the form: time value label 0 2020-01-01 -0.556014 high 1 2020-01-02 0.185451 high 2 2020-01-03 -0.401111 medium 3 2020-01-04 0.4

Solution 1:

I would create a column that increments on each change, then group by that column. If you need separate dataframes you can assign them in a loop.

df['group'] = df['label'].ne(df['label'].shift()).cumsum()
df = df.groupby('group')
dfs = []
for name, data in df:
    dfs.append(data)

dfs will be a list of dataframes like so:

[         time     value label  group
 0  2020-01-01 -0.556014  high      1
 1  2020-01-02  0.185451  high      1,
          time     value   label  group
 2  2020-01-03 -0.401111  medium      2
 3  2020-01-04  0.436111  medium      2,
          time     value label  group
 4  2020-01-05  0.412933  high      3
 5  2020-01-06  0.636421  high      3
 6  2020-01-07  1.168237  high      3
 7  2020-01-08  1.205073  high      3
 8  2020-01-09  0.798674  high      3
 9  2020-01-10  0.174116  high      3]

Post a Comment for "How To Split A Dataframe Each Time A String Value Changes In A Column?"