Consider the pd.Series
s
with pd.MultiIndex
mux
lol = [list('aabc'), list('xyyz'), [1, 2, 3, 3]]
mux = pd.MultiIndex.from_arrays(lol, names='l1 l2 l3'.split())
s = pd.Series(range(1001, 1005), mux)
s
l1 l2 l3
a x 1 1001
y 2 1002
b y 3 1003
c z 3 1004
dtype: int64
According to the documentation for unstack
fill_value : replace NaN with this value if the unstack produces missing values
And when I unstack
s.unstack()
l3 1 2 3
l1 l2
a x 1001.0 NaN NaN
y NaN 1002.0 NaN
b y NaN NaN 1003.0
c z NaN NaN 1004.0
I do get NaN
s.
So I try fill_value=0
s.unstack(fill_value=0)
l3 1 2 3
l1 l2
a x 1001 0 0
y 0 1002 0
b y 0 0 1003
c z 0 0 1004
Sure enough, the NaN
s were filled with 0
.
However, if I want to unstack
more that one level at a time.
s.unstack(['l2', 'l3'], fill_value=0)
l2 x y z
l3 1 2 3 3
l1
a 1001.0 1002.0 NaN NaN
b NaN NaN 1003.0 NaN
c NaN NaN NaN 1004.0
My fill_value
is ignored.
Why? And what is a work around?
Try this:
In [3]: s.unstack(['l2', 'l3']).fillna(0)
Out[3]:
l2 x y z
l3 1 2 3 3
l1
a 1001.0 1002.0 0.0 0.0
b 0.0 0.0 1003.0 0.0
c 0.0 0.0 0.0 1004.0
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments