简体   繁体   English

如何按列表删除 dataframe 中的多行?

[英]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.具体来说,我有一个棒球运动员从 1999 年到 2021 年的统计数据列表。如果一名球员在 1999 年打球,我想删除他们的所有行。 For example, if they played in 1999, 2000 and 2001, I want to drop all three of those rows.例如,如果他们在 1999 年、2000 年和 2001 年参加比赛,我想删除所有这三行。

在此处输入图像描述

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).我使用产生 dataframe 的 df[df['Season'].isin([1999])]['Name'] 识别玩家列表(如果需要,可以将其转换为列表)。

在此处输入图像描述

How do I now take my original dataframe and drop all the rows based on that list of names?我现在如何获取我原来的 dataframe 并根据该名称列表删除所有行?

The below code removes all the players participated in a specific season;以下代码删除了参加特定赛季的所有球员;

Original DF:原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: Output:

Season  Player
2   1997    C
3   1994    A
4   1997    C
6   1998    D
7   1997    C
8   1999    E

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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