Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

search for list of strings in pandas column

# Searching for strings and appending to dataframe.
search = ['FR-001', 'FR-002', 'FR-003', 'FR-004']
df['FR'] = df['Description'].str.findall('(' + '|'.join(search) + ')')
print (df)

                            Description                FR
0  AasfasfFR-001,asfasdfafsagsdg FR-002  [FR-001, FR-002]
1                 AasfasfFR-004, FR-002  [FR-004, FR-002]
2         AasfasfFR-02,asfasdfafsagsdg                 []
3  AasfasfFR-001,asfasdfafsagsdg FR-003  [FR-001, FR-003]
4  AasfasfFR-004,asfasdfafsagsdg FR-002  [FR-004, FR-002]

#To filter out empty list
df = df[df['FR'].astype(bool)]
print (df)

                            Description                FR
0  AasfasfFR-001,asfasdfafsagsdg FR-002  [FR-001, FR-002]
1                 AasfasfFR-004, FR-002  [FR-004, FR-002]
3  AasfasfFR-001,asfasdfafsagsdg FR-003  [FR-001, FR-003]
4  AasfasfFR-004,asfasdfafsagsdg FR-002  [FR-004, FR-002]
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #search #list #strings #pandas #column
ADD COMMENT
Topic
Name
5+3 =