Nick
Nick

Reputation: 394

Rows not sorted after concat

In the code below I'm attempting to reorder a portion of a dataframe and then join it with another portion. The code will sort but when I attempt to run the last line it returns the unsorted frame. Can anyone help with this?

Code

copied = frame[frame['PLAYVAL'].isin([3,4])].copy()
copied_col = copied['PLAY_EVT']

copied = copied.drop(columns=['PLAY_EVT'],axis=1)
copied = copied.sort_values(['TIME_ELAPSED','SHOTVAL'],ascending=[True,True]).copy()
result = pd.concat([copied_col,copied],axis=1)

Frame

PLAY_EVT TIME_ELAPSED INFO SHOTVAL
0 1 132 1of2 2
1 2 132 2of2 3
2 3 342 3of3 6
3 4 342 2of3 5
4 5 342 1of3 4
5 6 786 2of2 3
6 7 786 1of2 2

Expected Outcome

PLAY_EVT TIME_ELAPSED INFO SHOTVAL
0 1 132 1of2 2
1 2 132 2of2 3
2 3 342 1of3 4
3 4 342 2of3 5
4 5 342 3of3 6
5 6 786 1of2 2
6 7 786 2of2 3

Upvotes: 1

Views: 113

Answers (1)

Nick
Nick

Reputation: 394

Figured it out. Must've had something to do with indexes.

copied = frame[frame['PLAYVAL'].isin([3,4])]
copied_col = copied['PLAY_EVT'].reset_index(drop=True)

copied = copied.drop(columns=['PLAY_EVT'],axis=1)
copied = copied.sort_values(['TIME_ELAPSED','SHOTVAL'],ascending=[True,True]).reset_index(drop=True)
result = pd.merge(copied_col, copied, left_index=True, right_index=True)

Upvotes: 1

Related Questions