Skip to content
Advertisement

Pandas pivot table count

I would like to ask a question concerning pivot tables in Pandas.
I have been trying to make a pivot table for this kind of table:

sector score
US null
US null
US 1
EU null
EU 2
EU 2
EU 4
UK null
UK null
UK null
UK 4
UK 4

Eventually, I would like this table to be a pivot table that would look like this:

null 1 2 4
US 2 1 0 0
EU 1 0 2 1
UK 3 0 0 2

In order to do so, I have been trying to do as follows:

JavaScript

However, by doing so, I keep getting the following error:

JavaScript

Could you please help me?
Thanks :)

Advertisement

Answer

I prefer using groupby, you can use

JavaScript

Sidenote:

If you did not want to count the missing values,

JavaScript

would do the trick. Using len as an actual function because for some reason pivot_table‘s aggfunc parameter does not seem to accept string shortcuts like groupby.agg/transform.

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