Alex
Alex

Reputation: 1537

How to pivot and groupby PANDAS

How do I pivot and group by country this dataframe and use YEAR as column

pandas.DataFrame = (data  = {'COUNTRY': ['USA', 'USA', 'UK', 'UK'],
 'YEAR': [1999, 2000, 1999, 2000],
 'VALUE': [99, 111, 88, 100]} )

enter image description here

Such as I get this output:

enter image description here

Upvotes: 1

Views: 37

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195428

print(df.pivot(index="COUNTRY", columns="YEAR", values="VALUE").reset_index())

Prints:

YEAR COUNTRY  1999  2000
0         UK    88   100
1        USA    99   111

Upvotes: 2

Related Questions