如果值小于熊猫数据框系列中的先前值

时间:2018-06-21 18:25:34

标签: python pandas dataframe compare diff

这是我从日期中提取日期后的数据框的样子(请参见最后一列):

        Date       AAPL      NFLX       INTC  day
0 2008-01-02  27.834286  3.764286  25.350000    2
1 2008-01-03  27.847143  3.724286  24.670000    3
2 2008-01-04  25.721428  3.515714  22.670000    4
3 2008-01-07  25.377142  3.554286  22.879999    7
4 2008-01-08  24.464285  3.328571  22.260000    8

对于下一步,如果当前日期是<前一天,我想在另一列上添加一个带有true / false标记的“ Month_End”。此步骤是确定月底。这该怎么做?非常感谢。

到目前为止,我还没有运气尝试过以下方法(我是菜鸟,刚开始使用python)

for i, row in df.iterrows():
        if df.day.iloc[i+1] < df.day.iloc[i]:
            print (df['day'])

df.assign(Month_End = df.day.diff() < 0)
print(df.head())

2 个答案:

答案 0 :(得分:1)

您可以直接将日期与MonthEnd进行比较,看看它是否为True

from pandas import offsets
df['Month_End'] = df.Date == df.Date+offsets.MonthEnd(0)

示例:

df

        Date       AAPL      NFLX       INTC  day
0 2008-01-02  27.834286  3.764286  25.350000    2
1 2008-01-03  27.847143  3.724286  24.670000    3
2 2008-01-04  25.721428  3.515714  22.670000    4
3 2008-01-07  25.377142  3.554286  22.879999    7
4 2008-01-08  24.464285  3.328571  22.260000    8
5 2008-01-31  24.464285  3.328571  22.260000   31
6 2008-02-28  24.464285  3.328571  22.260000   28
7 2008-02-29  24.464285  3.328571  22.260000   29
8 2009-02-28  24.464285  3.328571  22.260000   28

from pandas import offsets
df.Date == df.Date + offsets.MonthEnd(0)

0    False
1    False
2    False
3    False
4    False
5     True
6    False
7     True
8     True
Name: Date, dtype: bool

答案 1 :(得分:1)

您可以这样做:

from pandas.tseries.offsets import MonthEnd
df['Month_End'] = np.where(pd.to_datetime(df['Date'])== pd.to_datetime(df['Date']) + MonthEnd(0),  True, False)

输出:

        Date       AAPL      NFLX       INTC  day  Month_End
0 2008-01-02  27.834286  3.764286  25.350000    2      False
1 2008-01-03  27.847143  3.724286  24.670000    3      False
2 2008-01-04  25.721428  3.515714  22.670000    4      False
3 2008-01-07  25.377142  3.554286  22.879999    7      False
4 2008-01-31  24.464285  3.328571  22.260000    8       True