0
$\begingroup$

So I am currently trying to sort through a data frame containing attribute classes and values of teams. However, my data has multiple rows of different classes and values of the same Team ID/Attribute ID. I was wondering if there was a faster way to get just the last row of each of the same Team IDs/Attribute IDs.

Snippet of the only row I want per team

$\endgroup$

1 Answer 1

0
$\begingroup$

Try pandas.DataFrame.drop_duplicates

DataFrame.drop_duplicates(subset=None, keep='first', inplace=False, ignore_index=False)

Return DataFrame with duplicate rows removed.

Considering certain columns is optional. Indexes, including time indexes are ignored.

Parameters
subsetcolumn label or sequence of labels, optional
Only consider certain columns for identifying duplicates, by default use all of the columns.

keep{‘first’, ‘last’, False}, default ‘first’
Determines which duplicates (if any) to keep. - first : Drop duplicates except for the first occurrence. - last : Drop duplicates except for the last occurrence. - False : Drop all duplicates.

inplacebool, default False
Whether to drop duplicates in place or to return a copy.

ignore_indexbool, default False
If True, the resulting axis will be labeled 0, 1, …, n - 1.

In your case

df_clean = df.drop_duplicates(subset=["Team ID", "Attribute ID"], keep="last")
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.