简体   繁体   English

Pandas:每组两列前向填充缺失值

[英]Pandas: Forward fill missing value by each group of two columns

I have data with group level as ['color', 'fruit', 'date', 'value'].我的组级别数据为 ['color', 'fruit', 'date', 'value']。

data = pd.DataFrame({'color': ['Green','Green', 'Green', 'Green', 'Red', 'Red'], 
                    'fruit' : ['Banana', 'Banana', 'Apple', 'Apple', 'Banana', 'Apple'],
                    'date': ['2011-01-01', '2011-01-02', '2011-01-01', '2011-01-02', '2011-02-01', '2011-02-01'],
                    'value': [ 1, np.nan, np.nan, 2, 3 , np.nan]})


Output:


Class   fruit   date    value
0   Green   Banana  2011-01-01  1.0
1   Green   Banana  2011-01-02  NaN
2   Green   Apple   2011-01-01  NaN
3   Green   Apple   2011-01-02  2.0
4   Yellow  Banana  2011-02-01  3.0
5   Yellow  Apple   2011-02-01  NaN

I need to fill down for 'value' where for a date we have no data.我需要在没有数据的日期填写“值”。 So this fill down would only be limited to ['color', 'fruit'] level.因此,此填充仅限于 ['color', 'fruit'] 级别。

I am trying to fill down with df = df.groupby(['color', 'fruit', 'date'])['value'].mean().replace(to_replace=0, method='ffill') but this spills the data over to next associated group of [color, fruit]我正在尝试填写df = df.groupby(['color', 'fruit', 'date'])['value'].mean().replace(to_replace=0, method='ffill')但是这会将数据溢出到下一个相关联的 [color, fruit] 组

Expected Output:


Class   fruit   date    value
0   Green   Banana  2011-01-01  1.0
1   Green   Banana  2011-01-02  1.0
2   Green   Apple   2011-01-01  NaN
3   Green   Apple   2011-01-02  2.0
4   Yellow  Banana  2011-02-01  3.0
5   Yellow  Apple   2011-02-01  NaN

You can use GroupBy.cumcount with pandas.Series.ffill :您可以将GroupBy.cumcountpandas.Series.ffill一起使用:

m = data.groupby(["color", "fruit"]).cumcount().astype(bool)

data["value"] = data["value"].ffill().where(m, data["value"])

Or as mentionned by @ Mustafa Aydin , simply use GroupBy.ffill :或者如@Mustafa Aydin所述,只需使用GroupBy.ffill

data["value"] = data.groupby(["color", "fruit"])["value"].ffill()

Output: Output:

print(data)

   color   fruit        date  value
0  Green  Banana  2011-01-01    1.0
1  Green  Banana  2011-01-02    1.0
2  Green   Apple  2011-01-01    NaN
3  Green   Apple  2011-01-02    2.0
4    Red  Banana  2011-02-01    3.0
5    Red   Apple  2011-02-01    NaN

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

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