0

我正在尝试使用以前的 Total 来填充 NA,然后减去 Change

            Change Total
01/01/2021  -12 100
02/01/2021  -54 154
03/01/2021  -23 177
04/01/2021  -2  NaN
05/01/2021  -54 NaN
06/01/2021  -72 NaN

期望的输出;

            Change Total
01/01/2021  -12 100
02/01/2021  -54 154
03/01/2021  -23 177
04/01/2021  -2  179
05/01/2021  -54 233
06/01/2021  -72 305

我尝试了各种操作 ffill() 的方法,但没有成功;

df['Total'] = df['Total'].fillna(method = 'ffill' - df['Change'])

有没有更好的方法来尝试这个?非常感谢任何帮助!

4

4 回答 4

3

预期的输出看起来像ffill减号cumsum。向前填充最后一个有效值,然后减去每个 NaN 行的累积总数:

# Select NaN rows
m = df['Total'].isna()
# Update NaN rows with the last valid value minus the current total Change
df.loc[m, 'Total'] = df['Total'].ffill() - df.loc[m, 'Change'].cumsum()

df

            Change  Total
01/01/2021     -12  100.0
02/01/2021     -54  154.0
03/01/2021     -23  177.0
04/01/2021      -2  179.0
05/01/2021     -54  233.0
06/01/2021     -72  305.0
于 2021-09-06T16:36:00.970 回答
2

让我们试试combine_first

df = df.combine_first(df[['Total']].ffill().sub(df.loc[df.Total.isnull(),'Change'].cumsum(),axis=0))
df
            Change  Total
01/01/2021     -12  100.0
02/01/2021     -54  154.0
03/01/2021     -23  177.0
04/01/2021      -2  179.0
05/01/2021     -54  233.0
06/01/2021     -72  305.0
于 2021-09-06T16:42:30.857 回答
2

您可以使用pd.Series.where

df["Total"] = df["Total"].where(df["Total"].notnull(),
                                df['Total'].ffill() - df.loc[df['Total'].isnull(), 'Change'].cumsum())

print (df)

            Change  Total
01/01/2021     -12  100.0
02/01/2021     -54  154.0
03/01/2021     -23  177.0
04/01/2021      -2  179.0
05/01/2021     -54  233.0
06/01/2021     -72  305.0
于 2021-09-06T16:26:52.217 回答
1

您可以使用np.wherefillna()如下:

change_when_total_null = df.loc[df['Total'].isnull(), 'Change']
df['Total'] = np.where(df['Total'].isnull(),df['Total'].fillna(method='ffill') -change_when_total_null.cumsum(),df['Total'])

印刷:

            Change  Total
01/01/2021     -12  100.0
02/01/2021     -54  154.0
03/01/2021     -23  177.0
04/01/2021      -2  179.0
05/01/2021     -54  233.0
06/01/2021     -72  305.0
于 2021-09-06T16:25:14.027 回答