简体   繁体   English

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

[英]Pandas fillna with datetime of another column, but with modification

I have a DataFrame: 我有一个DataFrame:

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

Out: 日期:

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

I'm trying to .fillna() by taking the value of Price, of the day before. 我正在尝试通过获取前一天的Price值来.fillna() So in this case, the NaN value will be filled with 100 , because we took the date of the NaN value minus one day. 因此,在这种情况下,NaN值将填充100 ,因为我们将NaN值的日期减去了一天。

Use: 采用:

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

If need repalce last non missing value (not day before): 如果需要代替最后一个非缺失值(不是前一天):

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

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

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