pandas fillna上的多索引系列 [英] Pandas fillna on a multiindex series

查看:78
本文介绍了 pandas fillna上的多索引系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有NA的series_A由MultiIndex (X, Y)索引,而要填充的值位于Series_B中,而Series_BX索引.如何有效解决此类问题?

The series_A with NAs is indexed by a MultiIndex (X, Y), while the values to fill in are in the Series_B, which is indexed by X. How to effectively approach this kind of problem?

例如,这是series_A:

bar  one    0.299368
     two         NaN
baz  one   -0.863838
     two   -0.251905
foo  one    1.063327
     two         NaN
qux  one    0.206053
     two    0.408204

series_B包含要填写的值:

bar  0.123
foo  0.456

推荐答案

方法1
使用unstack将第一级索引放入列中,然后使用fillna

Method 1
use unstack to put first level of index into columns and then use fillna

series_A.unstack(0).fillna(series_B).unstack().dropna()

bar  one    0.299368
     two    0.123000
baz  one   -0.863838
     two   -0.251905
foo  one    1.063327
     two    0.456000
qux  one    0.206053
quz  two    0.408204
dtype: float64

方法2
使用add方法来利用其levelfill_value参数,然后再使用combine_first

Method 2
use add method to leverage its level and fill_value parameters then combine_first

series_A.combine_first(series_A.add(series_B, level=0, fill_value=0))

bar  one    0.299368
     two    0.123000
baz  one   -0.863838
     two   -0.251905
foo  one    1.063327
     two    0.456000
qux  one    0.206053
quz  two    0.408204
dtype: float64

方法3
在可调用的series_B.get

Method 3
use map on the Index object returned from series_A.index.get_level_vaues(0) with the callable series_B.get

series_A.fillna(
    pd.Series(series_A.index.get_level_values(0).map(series_B.get), series_A.index)
)

bar  one    0.299368
     two    0.123000
baz  one   -0.863838
     two   -0.251905
foo  one    1.063327
     two    0.456000
qux  one    0.206053
quz  two    0.408204
dtype: float64

方法4
使用np.isnannp.flatnonzero查找np.nan所在的位置.然后找到要用get_level_valuesmap插入的值.最后,用iloc

Method 4
use np.isnan and np.flatnonzero to find the positions of where the np.nans are. Then find the values to insert with get_level_values and map. Finally, place into location with iloc

i = np.flatnonzero(np.isnan(series_A.values))
series_A.iloc[i] = series_A.index.get_level_values(0)[i].map(series_B.get)
series_A

bar  one    0.299368
     two    0.123000
baz  one   -0.863838
     two   -0.251905
foo  one    1.063327
     two    0.456000
qux  one    0.206053
quz  two    0.408204
dtype: float64

这篇关于 pandas fillna上的多索引系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆