Filter by string column as a substring of another string
Question:
I am trying to filter a dataframe by a string column. I would like the filter to return all rows where this string column is a substring of another string. Any searching I do for this problem leads to results about the converse – filtering a dataframe where a string columns contains a substring.
In other words, what I am attempting to achieve is:
df[df["string_column"] in "some_string"]
or
df[df["string_column"].str.is_substring_of("some_string")]
not
df[df["string_column"].str.contains("some_string")]
Answers:
df[df["string_column"].apply(lambda x: x in "some_string")]
df[[True if i in 'some_string' else False for i in df["string_column"]]]
I am trying to filter a dataframe by a string column. I would like the filter to return all rows where this string column is a substring of another string. Any searching I do for this problem leads to results about the converse – filtering a dataframe where a string columns contains a substring.
In other words, what I am attempting to achieve is:
df[df["string_column"] in "some_string"]
or
df[df["string_column"].str.is_substring_of("some_string")]
not
df[df["string_column"].str.contains("some_string")]
df[df["string_column"].apply(lambda x: x in "some_string")]
df[[True if i in 'some_string' else False for i in df["string_column"]]]