首页 > 解决方案 > 在大型数据框中去除间隔的(不一致的)开始和结束日期

问题描述

我有一个由 2 列组成的数据框。日期 - 季度日期 指标值 - 给定日期的范围从 1 到 -1。

Date(Quarter)   Indicator Value    

1872-12-31        0.5                  
1873-03-31       -0.2                  
1873-06-30       -0.15                 
1873-09-30        0.7                  
1873-12-31        0.4                  
1874-03-31        0.9                 
1874-06-30       -0.3                  
1874-09-30       -0.1                  
1874-12-31        0.8                  

我对负值循环感兴趣。我想获取所有负值的开始和结束日期,但不是单独获取。例如,

First one starts at 1873/01/01 and ends in 1873/06/30
The second one starts at 1874/04/01 and ends in 1874/09/30

我尝试创建二进制值。例如,所有正数仍然为零,负数将有 1。然后我可以去掉较小的数据框并获取日期。

看起来我让它看起来更难了。

def f(row):
    if row['Indicator value'] < 0:
        val = 1
    else:
        val = 0
    return val

df['Binary Value'] = df3.apply(f, axis = 1)

数据框看起来像这样:

    Date(Quaterly)   Indicator Value    Binary Value 

1872-12-31        0.5                  0
1873-03-31       -0.2                  1
1873-06-30       -0.15                 1
1873-09-30        0.7                  0
1873-12-31        0.4                  0
1874-03-31        0.9                  0
1874-06-30       -0.3                  1
1874-09-30       -0.1                  1
1874-12-31        0.8                  0

在这之后我有点卡住了。我不知道从这里去哪里。

我想创建一个开始和结束日期的列表

Start Date     End date 
1873/01/01     1873/06/30 
1874/04/01     1874/09/30

标签: pythonpandasdataframe

解决方案


g = (df['Indicator Value'].gt(0) == df['Indicator Value'].lt(0).shift()).cumsum()
g.name = 'value'

df = df.groupby(g).apply(lambda x: x.iloc[np.r_[0:1, -1:0]])

allneg = df[df['Indicator Value'].lt(0)].reset_index().drop(columns = ['value','level_1'])

pd.DataFrame(np.hstack([allneg.loc[::2, 'Date(Quarter)'].values.reshape(-1,1), allneg.loc[1::2, 'Date(Quarter)'].values.reshape(-1,1)]))\
  .rename(columns = {0:'Start Date', 1:'End Date'})

    Start Date   End Date
0   1873-03-31  1873-06-30
1   1874-06-30  1874-09-30

g 是我们希望为指标值列创建的分组。只要符号改变,值就会上升。

We then group by g, then apply a function that takes the first value and last value in each group (so if there are 3 or more neg values in a row it will only take the first and the last).

Next we filter out the positive values and do a little cleaning. Finally, we manipulate allneg to get just the starting and ending dates and make it a new dataframe.

For my start dates, I use the initial negative one, I'm not sure where you got yours from.


推荐阅读