有没有办法得到pandas中前两列的平均值?

Is there a way to get the mean value of previous two columns in pandas?

我想计算前两行的平均值并将 NAN 填充到我的数据框中。 2010-19 列中只有几行缺少值。

我尝试使用 bfillffill 但它只捕获前一个或下一个 row/column 值并填充 NAN。

我的示例数据集有 7 列,如下所示:

X       1990-2000   2000-2010   2010-19   1990-2000  2000-2010   2010-19
Hyderabad    10          20       NAN         1         3           NAN

我想要的输出:

X          1990-2000   2000-2010   2010-19   1990-2000  2000-2010   2010-19
Hyderabad    10          20          15         1           3         2

要以这种方式按行使用 fillna,一个简单的解决方案是提供一个 pandas 系列作为 fillna 的参数。这将根据索引替换 NaN 值。

由于列名重复,下面的代码使用列索引。假设一个名为 df:

的数据框
col_indices = [3, 6]

for i in col_indices:
    means = df.iloc[:, [i-1, i-2]].mean(axis=1)
    df.iloc[:, i].fillna(means, inplace=True)

这将用 col_indices 中每列左侧两列的平均值填充 NaN 值。