Python 熊猫比较下一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30673209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:48:37  来源:igfitidea点击:

Pandas compare next row

pythonpandasdataframetime-series

提问by NinjaGaiden

I have a dataframe like this

我有一个这样的数据框

d={}
d['z']=['Q8','Q8','Q7','Q9','Q9']
d['t']=['10:30','10:31','10:38','10:40','10:41']
d['qty']=[20,20,9,12,12]

I want compare first row with second row

我想比较第一行和第二行

  1. is qty same as next row AND
  2. is t greater in the next row AND
  3. is z value same as next row
  1. 与下一行数量相同 AND
  2. 在下一行中 t 更大 AND
  3. z 值是否与下一行相同

The desired value is

所需的值是

   qty                   t   z  valid
0   20 2015-06-05 10:30:00  Q8  False
1   20 2015-06-05 10:31:00  Q8   True
2    9 2015-06-05 10:38:00  Q7  False
3   12 2015-06-05 10:40:00  Q9  False
4   12 2015-06-05 10:41:00  Q9   True

采纳答案by firelynx

Looks like you want to use the Series.shiftmethod.

看起来您想使用该Series.shift方法。

Using this method, you can generate new columns which are offset to the original columns. Like this:

使用此方法,您可以生成与原始列偏移的新列。像这样:

df['qty_s'] = df['qty'].shift(-1)
df['t_s'] = df['t'].shift(-1)
df['z_s'] = df['z'].shift(-1)

Now you can compare these:

现在你可以比较这些:

df['is_something'] = (df['qty'] == df['qty_s']) & (df['t'] < df['t_s']) & (df['z'] == df['z_s'])

Here is a simplified example of how Series.shiftworks to compare next row to the current:

以下是Series.shift将下一行与当前行进行比较的工作原理的简化示例:

df = pd.DataFrame({"temp_celcius":pd.np.random.choice(10, 10) + 20}, index=pd.date_range("2015-05-15", "2015-05-24")) 
df
            temp_celcius

2015-05-15            21
2015-05-16            28
2015-05-17            27
2015-05-18            21
2015-05-19            25
2015-05-20            28
2015-05-21            25
2015-05-22            22
2015-05-23            29
2015-05-24            25

df["temp_c_yesterday"] = df["temp_celcius"].shift(1)
df
            temp_celcius  temp_c_yesterday
2015-05-15            21               NaN
2015-05-16            28                21
2015-05-17            27                28
2015-05-18            21                27
2015-05-19            25                21
2015-05-20            28                25
2015-05-21            25                28
2015-05-22            22                25
2015-05-23            29                22
2015-05-24            25                29

df["warmer_than_yesterday"] = df["temp_celcius"] > df["temp_c_yesterday"]
            temp_celcius  temp_c_yesterday warmer_than_yesterday
2015-05-15            21               NaN                 False
2015-05-16            28                21                  True
2015-05-17            27                28                 False
2015-05-18            21                27                 False
2015-05-19            25                21                  True
2015-05-20            28                25                  True
2015-05-21            25                28                 False
2015-05-22            22                25                 False
2015-05-23            29                22                  True
2015-05-24            25                29                 False

If I misunderstood your query, please post a comment and I'll update my answer.

如果我误解了您的查询,请发表评论,我会更新我的答案。