Pandas Dataframe中的bool值的条件正向填充 [英] Conditional forward fill of bool values in Pandas Dataframe

查看:504
本文介绍了Pandas Dataframe中的bool值的条件正向填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

如果第一天进入== True,如何将填充布尔True值转发到熊猫数据框中,直到一天结束

How do I forward fill bool True value in a pandas dataframe if first entry of day == True, to the end of a day

请参见以下示例和所需的输出.

Please see the following example and desired output.

数据:

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'bool_col':[True,False,False,True,False,False,False,False,False],
    'dates':pd.date_range('1/1/2011', periods=9, freq='8h')})


   bool_col dates
0   True    2011-01-01 00:00:00
1   False   2011-01-01 08:00:00
2   False   2011-01-01 16:00:00
3   True    2011-01-02 00:00:00
4   False   2011-01-02 08:00:00
5   False   2011-01-02 16:00:00
6   False   2011-01-03 00:00:00
7   False   2011-01-03 08:00:00
8   False   2011-01-03 16:00:00

所需的输出:

  bool_col  dates
0   True    2011-01-01 00:00:00
1   True    2011-01-01 08:00:00
2   True    2011-01-01 16:00:00
3   True    2011-01-02 00:00:00
4   True    2011-01-02 08:00:00
5   True    2011-01-02 16:00:00
6   False   2011-01-03 00:00:00
7   False   2011-01-03 08:00:00
8   False   2011-01-03 16:00:00

2011-01-012011-01-02上,我们可以看到Trueffill直到一天结束,但是在2011-01-03 00:00:00上是False,所以没有进行任何更改.

On 2011-01-01 and 2011-01-02 we can see True is ffill until end of day but on 2011-01-03 00:00:00 there is a False so no changes made.

我尝试了什么?

我尝试使用ffill,但无法确定如何以我指定的条件使用它.

I tried using ffill but can't work out how to use this with the criteria I specified.

推荐答案

transform 是一种有效的方法,可以基于groupby逻辑获得结果序列的数据帧大小.以下说明直译为每天分组,并查看bool_col的第一个元素,如果它是True,则整个分组为True,否则保留该分组".

transform is an efficient way to get a resulting series the size of the dataframe based on groupby logic. The instructions below literally translates to "group each day, and look at the first element of bool_col, if it's True, the entire group is True otherwise keep the group".

df.groupby(df.dates.dt.date).bool_col.transform(lambda g: True if g.iloc[0] else g)
Out[363]: 
0     True
1     True
2     True
3     True
4     True
5     True
6    False
7    False
8    False

这篇关于Pandas Dataframe中的bool值的条件正向填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆