I am trying to drop all rows that come after a row which has yes
inside the 'Ammend'
column
df:
Ammend
0 no
1 yes
2 no
3 no
4 yes
5 no
Required output df:
Ammend
0 no
1 yes
3 no
4 yes
Look at the following code:
df = df.drop(df[df['Amended' == 'yes']], inplace=True)
Returns a KeyError: False
error message
I have tried many different variations of this using different methods like .index.tolist()
and .loc
but I can't seem to figure it out anyway.
I have also tried truncate:
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])
This returns:
IndexError: index 1 is out of bounds for axis 0 with size 1
Try this
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
Output:
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
One way using pandas.Series.ne
with shift
trick:
s = df["Ammend"]
new_df = df[~s.ne(s.shift()).cumsum().duplicated(keep="first")]
print(new_df)
Output:
Ammend
0 no
1 yes
2 no
4 yes
5 no
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.