Using pivot on pandas introduces unwanted NaNs

I’m doing some basic data wrangling and counting the number of True’s and False’s that each version has in the data below.

Here’s my pandas dataframe (df):

    version  type    count 0   A        False   80 1   A        True    11 2   B        False   72 3   B        True    53 

I’m attempting to pivot my dataframe using:

DF = df.pivot(values='count',columns='type') 

But I get a bunch of NaNs between my rows: (current output)

type  False  True 0     80     NaN 1     NaN    11 2     72     NaN 3     NaN    53 

Here’s my desired output:

False  True 80     11 72     53 

For context, I will then take the proportion of True/False after summing the two columns.

I know this is pretty simple, but am new to python and online solutions haven’t quite given me solutions to this basic reshaping. Can anyone please explain what I’m doing wrong? Thank you in advance!

Add Comment
1 Answer(s)

Try:

df.pivot(values='count',columns='type', index = 'version').reset_index(drop = True) 

You need to specify index.

result:

type  False  True  0        80     11 1        72     53 
Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.