2
df=pd.DataFrame({'sym':['A', 'B', 'C', 'D'],'event':[['1','2', '3'], ['1'], ['2', '3'],['2']]} )

df

    sym event
0   A   [1, 2, 3]
1   B   [1]
2   C   [2, 3]
3   D   [2]

Event column is made up of lists of strings. I am trying to filter the event column for any rows that contain '3' so I am looking for index 0 and 2.

I know to use

["3" in df.event[0]]

for each row and I think a lambda function would push me over the finish line.

2
  • df[df.event.astype(str).str.contains('3')]? Or whats your desired output? Commented Feb 8, 2021 at 2:56
  • 1
    Thats it. Perfect. thx Commented Feb 8, 2021 at 3:04

3 Answers 3

4

Please try:

print(df[df.event.astype(str).str.contains(r'\b3\b')])



sym      event
0   A  [1, 2, 3]
2   C     [2, 3]
Sign up to request clarification or add additional context in comments.

2 Comments

this will filter the '33' as well ~
Thanks, edited to ensure only 3 is picked
3

Series.explode to split list-like values to rows

use explode to turn a list to row:

'3' in df['event'].explode().values

to find which row contains '3', use index:

idx = df['event'].explode() == '3'
df.loc[idx[idx].index]

3 Comments

thanks. so how would I filter the df rows rows where df['event'] contains "3"?
you can use the row's index. as explode keep the row's origin index.
you can also do: df['event'].explode().eq('3').any(level=0) with explode . Adding an alternative :)
2

Let us try

out = df[pd.DataFrame(df.event.tolist()).isin(['3']).any(1).values]
Out[78]: 
  sym      event
0   A  [1, 2, 3]
2   C     [2, 3]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.