I have a dataframe and I want to delete every row until a specific string is found.
I have tried a couple things but nothing seems to be working. Here is what I have tried:
df[((df.Plate != 'Group Summaries'))]
df.loc[: df[(df['Plate:'] == 'Group Summaries')].index[0], :]
df[(df['Plate:'] == "Group Summaries").idxmax():]
CodePudding user response:
Why don't you find the first index where this case appears and then delete all previous row?
import pandas as pd
# Data
df = pd.DataFrame(
{"Plate": ["a", "b", "a", "c", "a", "c", "c"]})
# First index where values is c
idx = df.index[df["Plate"].eq("c")].min()
# Drop previous rows
df = df[idx:]
CodePudding user response:
You can use:
df[df['Plate'].eq('Group Summaries').cummax()]
How it works:
- mark True the rows with "Group Summaries" in the "Plate" column
- forward the True to all next rows with
cummax
- use boolean indexing to select the rows