Skip to content
Advertisement

Pandas pivot_table gives KeyError

(I’m fairly new to Python and completely new to Pandas.)

I have software usage data in a tab-separated txt file like this:

JavaScript

The DataFrame is imported correctly, and groupby methods like this work all right:

JavaScript

However, when I’m trying to create a pivot table with this line:

JavaScript

I get

JavaScript

where the “key” is the first IP value – which should not be used as a key at all.

What am I doing wrong?

Advertisement

Answer

Use column names instead values:

JavaScript

Or use pd.crosstab:

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