[英]Groupby day to show True if identical consecutive values
Given the following DataFrame.给定以下 DataFrame。 How do I add a new column showing
True
in next row when two consecutive "y"
are seen in a single day in the val
column (else False
).当在
val
列中的一天内看到两个连续的"y"
时,如何在下一行添加一个显示True
的新列(否则为False
)。
Each day resets the logic.每天都会重置逻辑。
Essentially looking for two consecutive "y" in a single day then if next row is same day add a True
.本质上是在一天内寻找两个连续的“y”,然后如果下一行是同一天,则添加一个
True
。
Data数据
df_so = pd.DataFrame(
{"val": ["y", "y", "y", "n", "y", "y", "y", "y", "n", "n", "y", "y", "n"]},
index=pd.date_range(start="1/1/2018", periods=13, freq="8h"),
)
df_so
val
2018-01-01 00:00:00 y
2018-01-01 08:00:00 y
2018-01-01 16:00:00 y
2018-01-02 00:00:00 n
2018-01-02 08:00:00 y
2018-01-02 16:00:00 y
2018-01-03 00:00:00 y
2018-01-03 08:00:00 y
2018-01-03 16:00:00 n
2018-01-04 00:00:00 n
2018-01-04 08:00:00 y
2018-01-04 16:00:00 y
2018-01-05 00:00:00 n
Desired output would look like this:所需的 output看起来像这样:
val desired_col
2018-01-01 00:00:00 y False
2018-01-01 08:00:00 y False
2018-01-01 16:00:00 y True
2018-01-02 00:00:00 n False
2018-01-02 08:00:00 y False
2018-01-02 16:00:00 y False
2018-01-03 00:00:00 y False
2018-01-03 08:00:00 y False
2018-01-03 16:00:00 n True
2018-01-04 00:00:00 n False
2018-01-04 08:00:00 y False
2018-01-04 16:00:00 y False
2018-01-05 00:00:00 n False
You can use groupby().rolling()
here:您可以在这里使用
groupby().rolling()
:
target = 2
days = df_so.index.normalize()
df_so['out'] = (df_so['val'].eq('y')
.groupby(days)
.rolling(target, min_periods=0).sum()
.reset_index(level=0,drop=True)
.groupby(days).shift().eq(target)
)
Or with groupby().transform
:或使用
groupby().transform
:
target = 2
df_so['out'] = (df_so['val'].eq('y')
.groupby(df_so.index.normalize())
.transform(lambda x: x.rolling(target).sum().shift().eq(target))
)
Output: Output:
val out
2018-01-01 00:00:00 y False
2018-01-01 08:00:00 y False
2018-01-01 16:00:00 y True
2018-01-02 00:00:00 n False
2018-01-02 08:00:00 y False
2018-01-02 16:00:00 y False
2018-01-03 00:00:00 y False
2018-01-03 08:00:00 y False
2018-01-03 16:00:00 n True
2018-01-04 00:00:00 n False
2018-01-04 08:00:00 y False
2018-01-04 16:00:00 y False
2018-01-05 00:00:00 n False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.