'Filter dataframe using startswith twice
I have a dataframe that I want to filter using startswith twice. Something like this
df = df.loc[df['Col'].str.startswith('Foo', na=False) | df['Col'].str.startswith('Bar', na=False)]
But this not work, how can I fix?
Solution 1:[1]
There are two ways to solve your problem. The first is to use paranthesis:
df = df.loc[(df['Col'].str.startswith('Foo', na=False)) | (df['Col'].str.startswith('Bar', na=False))]
The second is to make use of the string startswith method, that can take any number of arguments:
df.loc[df['Col'].str.startswith(('foo', 'bar'))]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user7375116 |
