I'd like to drop a bunch of rows based on a list of values. Specifically, I have a list of baseball player's stats from 1999 to 2021. If a player played in 1999, I want to drop all their rows. For example, if they played in 1999, 2000 and 2001, I want to drop all three of those rows.
I identify the list of players using the df[df['Season'].isin([1999])]['Name'] which produced a dataframe (that can be turned into a list if needed).
How do I now take my original dataframe and drop all the rows based on that list of names?
CodePudding user response:
The below code removes all the players participated in a specific season;
Original DF:
Season Player
0 2000 B
1 1993 B
2 1997 C
3 1994 A
4 1997 C
5 2000 F
6 1998 D
7 1997 C
8 1999 E
using below code, we can eliminate the rows
a = df[df['Season'].isin([2000])]['Player'].values
df = df.loc[~df['Player'].isin(a), :]
df
Output:
Season Player
2 1997 C
3 1994 A
4 1997 C
6 1998 D
7 1997 C
8 1999 E
CodePudding user response:
You can just slice the qualified rows as follows:
new_df = df[~df['Season'].isin([1999])]