how do I fill na values when I unstack more than one level at a time

piRSquared

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 NaNs.
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 NaNs 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?

MaxU

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.

edited at
0

Comments

0 comments
Login to comment

Related

How do I print values only when they appear more than once in a list in python

How do I look for more than one ID

How can I have more than one imageview on the screen at the same time when I click the screen? (Circle one imageview)

How do I restrict events firing more than once at a time

How do I get rid of a Bluebird warning when chaining more than one flatMap operator in RxJS?

How do I fill NA values in multiple columns in pandas?

How do I attach more than one stylesheet to an rmarkdown document?

How do I find available rooms using LINQ, when there are more than one of each room type?

In Linq, how do I add type checking to 'include clauses' more than one level deep?

How do I call more than one function for each case when using a "when" statement in Kotlin?

How can I enter more than five values on one line?

How do I find() an object property the matches values from more than one specified array

How can I .Include down more than one level in LINQ?

How do I run more than one Windows Explorer?

How do I chain together several [ ] [ ] with more than one key

How do I check in GSP Grails more than one time in list

How do I use more than one fxml with javaFX?

how do I change just one character in a string when there more than one of that character?

how do i access a param on more than one post or get

How do I specify which driver is used for a device when more than one are applicable

How do I filter with more than one letter in primefaces selectonemenu?

How do I play more than one video with AvPlayer?

How do I set "all windows" by default when left clicking on a application that has more than one window?

How Do I Use More Than One Style On HTML Label?

How do I specify more than one type to decode in the enum CodingKeys when using Codable?

How do I connect kqlmagic to more than one Log Analytics workspace at the same time?

(C++) How do I display an error when more than one "." is used in a calcualtor?

How do I activate more than one modalbox with js?

How do I display the updated element information when there is more than one element in the arrayList?