2021年2月7日星期日

Filtering a column of lists of strings in a Pandas DataFrame

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.

https://stackoverflow.com/questions/66095340/filtering-a-column-of-lists-of-strings-in-a-pandas-dataframe February 08, 2021 at 10:53AM

没有评论:

发表评论