如何将 pandas fillna NaN 与下一行值的负值一起使用

how to use pandas fillna NaN with the negative of the next row value

我有 table 城市的每日(时间序列)降雨。 pandas同城次日雨的负数如何用pandas填NaN?谢谢你。

import pandas as pd
import numpy as np
rain_before = pd.DataFrame({'date':Date*2,'city':list('aaaaabbbbb'),'rain':[6,np.nan,1,np.nan,np.nan,4,np.nan,np.nan,8,np.nan]})

# after fillna, the table should look like this. 
rain_after_fillna = pd.DataFrame({'date':Date*2,'city':list('aaaaabbbbb'),'rain':[6,-1,1,np.nan,np.nan,4,np.nan,-8,8,np.nan]})


你可以移动和填充

rain_before['rain'].fillna(rain_before.groupby('city')['rain']
                           .transform(lambda x: -x.shift(-1)))

0    6.0
1   -1.0
2    1.0
3   NaN 
4   NaN 
5    4.0
6   NaN 
7   -8.0
8    8.0
9   NaN 
Name: rain, dtype: float64

使用shift(-1)*-1的系列。没有样本数据集,所以我综合了但不包括城市。同样的方法可以用于城市,需要考虑排序

import datetime as dt
import random
df = pd.DataFrame({"Date":pd.date_range(dt.date(2021,1,1), dt.date(2021,1,10))
             ,"rainfall":[i*random.randint(0,1) for i in range(10)]}).replace({0:np.nan})

df["rainfall_nan"] = df["rainfall"].fillna(df["rainfall"].shift(-1)*-1)

输出

      Date  rainfall  rainfall_nan
2021-01-01       NaN          -1.0
2021-01-02       1.0           1.0
2021-01-03       2.0           2.0
2021-01-04       3.0           3.0
2021-01-05       NaN          -5.0
2021-01-06       5.0           5.0
2021-01-07       6.0           6.0
2021-01-08       7.0           7.0
2021-01-09       NaN          -9.0
2021-01-10       9.0           9.0