pandas fillna上的多索引系列 [英] Pandas fillna on a multiindex series
问题描述
具有NA的series_A
由MultiIndex (X, Y)
索引,而要填充的值位于Series_B
中,而Series_B
由X
索引.如何有效解决此类问题?
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
方法来利用其level
和fill_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.isnan
和np.flatnonzero
查找np.nan
所在的位置.然后找到要用get_level_values
和map
插入的值.最后,用iloc
Method 4
use np.isnan
and np.flatnonzero
to find the positions of where the np.nan
s 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屋!