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 indf:
dfs.append(data)
dfs will be a list of dataframes like so:
[ timevaluelabelgroup02020-01-01 -0.556014high112020-01-02 0.185451high1,
timevaluelabelgroup22020-01-03 -0.401111medium232020-01-04 0.436111medium2,
timevaluelabelgroup42020-01-05 0.412933high352020-01-06 0.636421high362020-01-07 1.168237high372020-01-08 1.205073high382020-01-09 0.798674high392020-01-10 0.174116high3]
Post a Comment for "How To Split A Dataframe Each Time A String Value Changes In A Column?"