I have a complex, large pandas dataframe with one column, X that can contain either one list or a list of lists. I'm curious if the solution can apply to any content though, so I give a mock example with one element of X being a string as well:
df1 = pd.DataFrame({
'A': [1, 1, 3],
'B': ['a', 'e', 'f'],
'X': ['something', ['hello'], [['something'],['hello']]]}
)
I want to get the subset of that dataframe, df2, for which column X contains the substring "hello", when whatever is in there is read as a string.
>>> df2
A B X
0 1 e [hello]
1 3 f [[something], [hello]]
I have tried extensive combinations of str() and .str.contains, apply, map, .find(), list comprehensions, and nothing seems to work without getting into loops (related questions here and here. What am I missing?