Issue
I want to select the rows from dataframe that have "apple" in their column, but the "PHRASE" column should not be considered as string but as a list with "|" separator. Phrase column can have None/NaN values.
import pandas as pd
df = pd.DataFrame({"PHRASE": ["apple and bannaa|bannana", None, "apple", "orange|bannana", "orange|apple|bannana"]})
print(df)
0 apple and bannaa|bannana
1 apple
2 orange|bannana
3 orange|apple|bannana
I want to select rows that have apple in them, but the row should be considered as list with "|" as separator.
Desired output:
1 apple
3 orange|apple|bannana
I tried to do this
result = df[df.PHRASE.str.split("|").str.contains("apple")]
but it does not work.
Solution
You can still use str.contains
but with a regex pattern
df[df['PHRASE'].str.contains(r'\bapple(?:\||$)', na=False)]
PHRASE
1 apple
3 orange|apple|bannana
Answered By - Shubham Sharma
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.