Reputation: 137
I have an excerpt of a dataframe df
below:
Player Trial Score
0 Josh 1 1
1 Josh 2 3
2 Josh 3 2
3 Josh 4 2
4 Josh 5 2
5 Josh 6 5
6 Josh 7 2
0 Yuli 1 1
1 Yuli 2 3
2 Yuli 3 2
3 Yuli 4 2
4 Yuli 5 2
0 Max 1 4
1 Max 2 6
2 Max 3 10
3 Max 4 10
4 Max 5 17
5 Max 6 10
6 Max 7 14
I would like to select the last rows which has a unique value for the 'Player' column i.e.
Player Trial Score
0 Max 1 4
1 Max 2 6
2 Max 3 10
3 Max 4 10
4 Max 5 17
5 Max 6 10
6 Max 7 14
I have tried df.groupby("Player").tail()
but this only returns the last 5 rows for each player. I have also tried a rather lengthy method:
df.groupby('Trial').tail().loc[df['Player'] == df['Player'].unique()[-1]]
This gives me what I want but I am sure I am missing a cleaner method. Any help would be appreciated to achieve this. Thank you.
Upvotes: 0
Views: 962
Reputation: 13821
If i understand correctly you just want to get all the rows of the final player (i.e the name in the last row of your df
in Player column.
last_player = df.Player.iloc[-1]
df.loc[df.Player==last_player]
which prints:
Player Trial Score
7 Max 1 4
8 Max 2 6
9 Max 3 10
10 Max 4 10
11 Max 5 17
12 Max 6 10
13 Max 7 14
Be aware the sorting your df
, in a different way, will highly likely alter the output of the above code.
Upvotes: 1