如何用Pandas DatatFrame中的行总和替换NaN
我正在尝试将某些列中的NaN替换为Pandas DataFrame中行的总和.参见下面的示例数据:
I am trying to replace the NaN in certain columns with the sum of the row in a Pandas DataFrame. See below the example data:
Items| Estimate1| Estimate2| Estimate3|
Item1| NaN | NaN | 8
Item2| NaN | NaN | 5.5|
我希望获得估算值1& 2对于项目1和2分别为8和5.5.
I am hoping to have Estimate 1 & 2 to be 8 and 5.5 for Item 1 and 2 respectively.
到目前为止,我已经尝试使用df.fillna(df.sum(), inplace=True)
,但是DataFrame中没有任何更改.谁能帮助我更正我的代码或推荐正确的方法?
So far I have tried using df.fillna(df.sum(), inplace=True)
but there is no change in the DataFrame. Can anyone assist me correct my code or recommend the right way to do it?
提供axis=1
似乎无效(因为用Series填充仅适用于逐列的情况,不适用于逐行的情况).
一种解决方法是将每一行的总和广播"到与原始索引/列具有相同索引/列的数据帧.稍微修改一下示例数据框:
Providing axis=1
does not seem to work (as filling with a Series only works for the column-by-column case, not for row-by-row).
A workaround is to 'broadcast' the sum of each row to a dataframe that has the same index/columns as the original one. With a slightly modified example dataframe:
In [57]: df = pd.DataFrame([[np.nan, 3.3, 8], [np.nan, np.nan, 5.5]], index=['Item1', 'Item2'], columns=['Estimate1', 'Estimate2', 'Estimate3'])
In [58]: df
Out[58]:
Estimate1 Estimate2 Estimate3
Item1 NaN 3.3 8.0
Item2 NaN NaN 5.5
In [59]: fill_value = pd.DataFrame({col: df.sum(axis=1) for col in df.columns})
In [60]: fill_value
Out[60]:
Estimate1 Estimate2 Estimate3
Item1 11.3 11.3 11.3
Item2 5.5 5.5 5.5
In [61]: df.fillna(fill_value)
Out[61]:
Estimate1 Estimate2 Estimate3
Item1 11.3 3.3 8.0
Item2 5.5 5.5 5.5
对此存在一个开放的增强问题: https://github.com/pydata/pandas /issues/4514
There is an open enhancement issue for this: https://github.com/pydata/pandas/issues/4514