简体   繁体   中英

How can I drop multiple rows in my dataframe by list?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM