简体   繁体   English

熊猫:回填DataFrameGroupBy对象

[英]Pandas: Backfilling a DataFrameGroupBy object

I have a DataFrame object that looks like this: 我有一个看起来像这样的DataFrame对象:

Out[9]:         pts   res  sensor
        0         0     Y   accel
        1         0   NaN   accel
        2         0     N    beta
        3         0   NaN    beta
        4         5   NaN    beta
        5         8   NaN   accel

I would like to write some code that first uses the .groupby() function to group by sensor . 我想写一些首先使用.groupby()函数按sensor分组的代码。 And then backfills the pts column of each group and forward fills the res column of each group. 然后回填每个组的pts列,并向前填充每个组的res列。 My code attempt looks like this: 我的代码尝试如下所示:

df_g = df.groupby('sensor')
next_pts = pd.Series(df_g.pts.bfill())
next_res = pd.Series(df_g.res.ffill())

df['next_pts'] = next_pts
df['next_res'] = next_res
df

The output was this: 输出是这样的:

Out[11]:         pts  res  sensor next_pts next_res
        0         0     Y   accel       0         Y
        1         0   NaN   accel       0         Y
        2         0     N    beta       0         N
        3         0   NaN    beta       0         N
        4         5   NaN    beta       5         N
        5         8   NaN   accel       8         Y

So it seems like the ffill() on the res column worked, but the bfill() on the pts column didn't. 因此,看起来res列上的ffill()起作用了,而pts列上的bfill()却没有bfill() How do I make it look like this?: 如何使它看起来像这样?:

Out[12]:         pts  res  sensor next_pts next_res
        0         0     Y   accel       8         Y
        1         0   NaN   accel       8         Y
        2         0     N    beta       5         N
        3         0   NaN    beta       5         N
        4         5   NaN    beta       5         N
        5         8   NaN   accel       8         Y

I found this Stack Overflow link that asks a similar question but on a DataFrame object, not a DataFrameGroupBy object: How to copy pandas DataFrame values down to fill 0's? 我发现这个Stack Overflow链接问一个类似的问题,但是在一个DataFrame对象上,而不是一个DataFrameGroupBy对象上: 如何将pandas DataFrame值复制下来以填充0?

But when I tried to do something like this on my DataFrameGroupBy object, it threw an error: Cannot access callable attribute 'astype' of 'SeriesGroupBy' objects, try using the 'apply' method 但是,当我尝试在DataFrameGroupBy对象上执行DataFrameGroupBy ,它引发了一个错误: Cannot access callable attribute 'astype' of 'SeriesGroupBy' objects, try using the 'apply' method

Any help would be so much appreciated!! 任何帮助将不胜感激!

It doesn't look like it has a lot to do with groupby , but rather with the ?fill functions, which don't fill int series with 0s. 看起来它与groupby并没有多大关系,而是与?fill函数有关,后者不会将int系列填充为0。

Perhaps there's a more elegant way to do it, but this worked: 也许有一种更优雅的方式可以做到这一点,但这确实可行:

>> df.pts = np.where(df.pts == 0, np.NaN, df.pts)
>> df.pts.groupby(df.sensor).apply(lambda g: g.bfill())
0    8
1    8
2    5
3    5
4    5
5    8
dtype: float64

Note that you can convert the float series back to ints easily using .astype . 请注意,您可以使用.astype将float系列轻松转换回int。

Edit The first line can be written as 编辑第一行可以写成

>> df.pts.replace(0, np.NaN)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM