繁体   English   中英

删除包含特定值的行之后的 Pandas DataFrame 行

[英]Drop a pandas DataFrame row that comes after a row that contains a particular value

我正在尝试删除在'Ammend'列中为yes的行之后的所有行

df:

  Ammend
 0  no
 1  yes
 2  no
 3  no
 4  yes
 5  no

所需的输出 df:

  Ammend
 0  no
 1  yes
 3  no
 4  yes

看下面的代码:

df = df.drop(df[df['Amended' == 'yes']], inplace=True)

返回KeyError: False错误消息

我已经使用.index.tolist().loc等不同方法尝试了许多不同的变体,但我似乎无法弄清楚。

我也试过截断:

filings_df.truncate(after=filings_df.loc[filings_df['Filings'] == '10-K/A'].index[0], before = filings_df.loc[filings_df['Filings'] == '10-K/A'].index[1])

这将返回:

索引错误:索引 1 超出轴 0 的范围,大小为 1

尝试这个

import pandas as pd
import numpy as np

np.random.seed(525)
df = pd.DataFrame({'Other': np.random.rand(10), 'Ammend': np.random.choice(['yes', 'no'], 10)})
df
      Other Ammend
0  0.750282     no
1  0.379455     no
2  0.766467    yes
3  0.351025     no
4  0.965993     no
5  0.709159     no
6  0.838831    yes
7  0.218321     no
8  0.573360    yes
9  0.738974     no

输出:

df.drop(index=df[df['Ammend'].shift() == 'yes'].index)

      Other Ammend
0  0.750282     no
1  0.379455     no
2  0.766467    yes
4  0.965993     no
5  0.709159     no
6  0.838831    yes
8  0.573360    yes

使用带有shift技巧的pandas.Series.ne一种方法:

s = df["Ammend"]
new_df = df[~s.ne(s.shift()).cumsum().duplicated(keep="first")]
print(new_df)

输出:

  Ammend
0     no
1    yes
2     no
4    yes
5     no

暂无
暂无

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

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