Skip to content
Advertisement

NaN values in pivot_table index causes loss of data

Here is a simple DataFrame:

JavaScript

Pivot method 1

The data can be pivoted to this:

JavaScript

Downside: data in the 2nd row is lost because df['b'][1] == None.

Pivot method 2

JavaScript

Downside: column b is lost.

How can the two methods be combined so that columns b and the 2nd row are kept like so:

JavaScript

More generally: How can information from a row be retained during pivoting if a key has NaN value?

Advertisement

Answer

Use set_index and unstack to perform the pivot:

JavaScript

This is essentially what pandas does under the hood for pivot. The stack and unstack methods are closely related to pivot, and can generally be used to perform pivot-like operations that don’t quite conform with the built-in pivot functions.

The resulting output:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement