How to subset multiple pandas data frames from a single time series data frame using row level iteration?

GaRaGe

I have a pandas data frame with 2 columns (time series date and value)

Input data frame:

date Value
2021-05-01 -2
2021-05-02 3
2021-05-03 5
2021-05-04 4
2021-05-05 6
2021-05-06 -3
2021-05-07 -8
2021-05-08 -1
2021-05-09 5
2021-05-10 4
2021-05-11 5
2021-05-12 1
2021-05-13 -1
2021-05-14 -2
2021-05-15 -1

I need to subset 2 data frames from this one. The condition is I need to loop through the rows and subset all the positive values in the same order along with 1 row before and after, that has negative values.

My expected outputs are below

Output data frame1

date Value
2021-05-01 -2
2021-05-02 3
2021-05-03 5
2021-05-04 4
2021-05-05 6
2021-05-06 -3

Output data frame2:

date Value
2021-05-08 -1
2021-05-09 5
2021-05-10 4
2021-05-11 5
2021-05-12 1
2021-05-13 -1

Any suggestions on how to do this in a most efficient manner? This is a sample data, but i might have a much longer series to be considered.

jezrael

You can filter before and after values less like 0 and create list of DataFrames in list comprehension:

m0 = df['Value'].lt(0)
m1 = m0 & df['Value'].shift(-1).ge(0)
m2 = m0 & df['Value'].shift().ge(0)

df['g'] = m1.cumsum()
df2 = df[m1 | m2 | ~m0].copy()

dfs = [g.drop('g', axis=1) for i, g in df2.groupby('g')]
print (dfs)
[         date  Value
0  2021-05-01     -2
1  2021-05-02      3
2  2021-05-03      5
3  2021-05-04      4
4  2021-05-05      6
5  2021-05-06     -3,           date  Value
7   2021-05-08     -1
8   2021-05-09      5
9   2021-05-10      4
10  2021-05-11      5
11  2021-05-12      1
12  2021-05-13     -1]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to create multiple Pandas time series from a data frame using python?

Using lapply to subset a single data frame into a list of data frames in R

create a multiple data frames from a single data frame in r

How do I create multiple new data frames in R, derived from a single data frame and named sequentially?

Convert hourly time series data frames to multiple data frames of single hours

How to split a pandas data frame into multiple data frames based on ID?

Merging data frames to plot multiple time series using plyr

Combining time series data into a single data frame

How to operate over subset of row on pandas data frame?

Subset common rows from multiple data frames

Pandas: How to merge two data frames and fill NaN values using values from the second data frame

In Pandas, how to get multiple subset data frame based on groupby criteria?

Pandas: Merge two data frames and keep non-intersecting data from a single data frame

Pandas: Convert single row in data frame to a list with multiple dimensions

How to subset a list of data frames using a vector

How to drop multiple columns from a data frame using pandas?

How to convert pandas single column data frame to series or numpy vector

How to subset a data frame using Pandas based on a group criteria?

How to combine multiple conditions to subset a data-frame using "OR"?

Iteration for time series data, using purrr

How to merge multiple rows in time series data having same time into a single record using scripting in Linux?

Subset data from the last time a condition is met till the last row of a data frame - applied to each subject

Convert row names in multiple data frames to column in data frame

getting a subset of arrays from a pandas data frame

How to plot a Pandas data frame with time series as rows?

how to get tsclean working on data frame with multiple time series

How to get bar plot for pandas time series data frame using Bokeh?

How to transform recurrent time series pandas data frame to pandas multi-index data frame

Delete series value from row of a pandas data frame based on another data frame value