Home > Software engineering >  create new dataframe once time-delta is higher than xy
create new dataframe once time-delta is higher than xy

Time:09-23

I have a dataframe with the following scheme:

time parameter TimeDelta
1 800 -
3 788 2
4 544 1
7 344 3
8 244 1

Is it possible to create new dataframes according TimeDelta? So if TimeDelta is greater than e.g. 1.5, create new dataframe? So that I have three dataframes as follows:

dataframe1

time parameter TimeDelta
1 800 -

dataframe2

time parameter TimeDelta
3 788 2
4 544 1

dataframe3

time parameter TimeDelta
7 344 3
8 244 1

CodePudding user response:

You can use a custom group and split with groupby.

First ensure that your "TimeDelta" values are numeric with pd.to_numeric, then asses whether they are geater than 1.5, and apply a cumsum() to flag all the following rows up to the next value above threshold. Finally groupby the custom group and convert to dict.

group = pd.to_numeric(df.TimeDelta, errors='coerce').gt(1.5).cumsum()
my_dfs = dict(list(df.groupby(group)))

output:

>>> my_dfs
{0:    time  parameter TimeDelta
 0     1        800         -,
 1:    time  parameter TimeDelta
 1     3        788         2
 2     4        544         1,
 2:    time  parameter TimeDelta
 3     7        344         3
 4     8        244         1}

accessing a particular group:

>>> my_dfs[1] # second group
   time  parameter TimeDelta
1     3        788         2
2     4        544         1

looping over the dataframes:

for group, df in dict(list(df.groupby(group))).items():
    print(f'group {group}')
    print(df)
  • Related