带有另一列日期时间的Pandas fillna,但有修改

时间:2019-02-27 09:22:48

标签: python pandas

我有一个DataFrame:

pd.DataFrame({"date": ["2018-12-21", "2018-12-22", "2018-05-04"], "price":[100,np.nan, 105]})

出局:

date       price
2018-12-21  100.0
2018-12-22  NaN
2018-05-04  105.0

我正在尝试通过获取前一天的价格来.fillna()。因此,在这种情况下,NaN值将用100填充,因为我们将NaN值的日期减去了一天。

1 个答案:

答案 0 :(得分:2)

使用:

df = pd.DataFrame({"date": ["2018-12-21", "2018-12-22", 
                            "2018-05-04","2018-05-05",
                            "2018-05-06","2018-05-09"], 
                   "price":[100,np.nan, 105, np.nan, 108, np.nan]})

print (df)

         date  price
0  2018-12-21  100.0
1  2018-12-22    NaN
2  2018-05-04  105.0
3  2018-05-05    NaN
4  2018-05-06  108.0
5  2018-05-09    NaN

df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')

df['price'] = df['price'].combine_first(df['price'].shift(1, freq='d'))
#alternative
#df['price'] = df['price'].combine_first(df['price'].shift(1, freq='d'))
print (df)
            price
date             
2018-12-21  100.0
2018-12-22  100.0
2018-05-04  105.0
2018-05-05  105.0
2018-05-06  108.0
2018-05-09    NaN

如果需要替换上一个非缺失值(不是前一天):

df['price'] = df['price'].ffill()
print (df)
         date  price
0  2018-12-21  100.0
1  2018-12-22  100.0
2  2018-05-04  105.0
3  2018-05-05  105.0
4  2018-05-06  108.0
5  2018-05-09  108.0