Skip to content
Advertisement

Restructure Pandas DataFrame

I have the following DataFrame:

play_id position frame x y
1 A_1 1 0.1 0.1
1 A_2 1 0.1 0.1
1 B_1 1 0.1 0.1
1 A_1 2 0.1 0.1
1 A_2 2 0.1 0.1
1 B_1 2 0.1 0.1
2 A_1 1 0.1 0.1
2 B_1 1 0.1 0.1
2 B_2 1 0.1 0.1
2 A_1 2 0.1 0.1
2 B_1 2 0.1 0.1
2 B_2 2 0.1 0.1

And I want to reformat to (Multi-Index columns):

position A_1 A_1 A_1 A_1 A_2 A_2 A_2 A_2 B_1 B_1 B_1 B_1 B_2 B_2 B_2 B_2
coord x x y y x x y y x x y y x x y y
frame 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2
play_id
1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 NaN NaN NaN NaN
2 0.1 0.1 0.1 0.1 NaN NaN NaN NaN 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1

Importantly, note that not all positions exist for all play_ids. This will result in some cells being empty.

Advertisement

Answer

  • sort_values() so index is in order you want
  • set_index() existing columns
  • sick() the coords
  • name everything
  • unstack() to get multi-index columns
JavaScript

output

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