python - searching a string pattern from a Data-frame column in pandas -
continuing last question in stack searching matching string pattern dataframe column in python pandas
suppose have dataframe
name genre satya |action|drama|ic| satya |comedy|drama|social|music| abc |drama|action|biopic| xyz |action||romance|darma| def |action|sport|comedy|ic| ghj |ic|actiondrama|noaction|
from answer of last question , able search 1 genre (ex ic) if independently exist in genre column , not part of other genre string value (music or biopic).
now want find if action , drama both present in genre column not in particular order , not part of string individually.
so need rows in output row[1,3,4]
name genre satya |action|drama|ic| # both adjacently present #row 2 not come # drama present not action abc |drama|action|biopic| ### both adjacently present in diff. order xyz |action||romance|darma| ### both present not adjacent ##row 5 should not present drama not here ## row 6 should not come both not present individually(but present 1 string part)
i tried
x = df[df['gen'].str.contains('\|action\|drama\|')] ### got row 1 (action , drama in adjacent , in order action->drama)
please suggest can followed/added here can need here.
i think can use str.contains
2 conditions , - &
:
print df name genre 0 satya |action|drama|ic| 1 satya |comedy|drama|social|music| 2 abc |drama|action|biopic| 3 xyz |action||romance|drama| 4 def |action|sport|comedy|ic| 5 ghj |ic|actiondrama|noaction| print df['genre'].str.contains('\|action\|') & df['genre'].str.contains('\|drama\|') 0 true 1 false 2 true 3 true 4 false 5 false name: genre, dtype: bool print df[ df['genre'].str.contains('\|action\|') & df['genre'].str.contains('\|drama\|') ] name genre 0 satya |action|drama|ic| 2 abc |drama|action|biopic| 3 xyz |action||romance|drama|
Comments
Post a Comment