Trying to filter out rows in which the data of specific column start with a given substring.
I have a pandas.DataFrame as shown below (simplified):
| price | DRUG_CODE |
|---|---|
| 123 | A12D958 |
| 234 | B564F3C |
| ... | ... |
I'm trying to filter out rows in which the DRUG_CODE does not start with the substring B21. However, most of the articles I found online about filtering DataFrames using substrings focus on identifying those that contain the substring, allowing it to appear anywhere within the cell (at the beginning, middle, or end)(eg: .str.contains() method). This doesn't align with my current requirement.
out = df[df['DRUG_CODE'].str.startswith('B21')]out = df[~df['DRUG_CODE'].str.startswith('B21')]to inverse the logic