简体   繁体   中英

How to check if a string column contains a substring and return row data if True?

I have two separate dataframes, one with the substrings that I would like to check and see if they are contained in the 2nd data frame that contains the string and row data. This code will only run weekly so I was not worried about optimization at the moment I attempted to do it with nested for loops but couldn't seem to solve it. For example purposes I created the following bellow, however the substring could be at the start, middle, and end of the string - Example:

map_df['Number_1'] = [1,2,3,4,5,...,n]
map_df['String'] = ['xxhello', 'randomyy', 'zztodayzz',...,n]
substring_df['Substring'] = ['hello', 'random', 'today', 'dog', 'cat',..., n]

##Desired result
Substring_df

['Substring']      ['Number_1']
hello                1
random               2
today                3
dog                  
cat
df = pd.DataFrame({'map_df_string': ['xxhello', 'randomyy', 'zztodayzz'], 'substring_df_substring': ['hello', 'random', 'today']})

OUTPUT:


    map_df_string   substring_df_substring
0   xxhello         hello
1   randomyy        random
2   zztodayzz       today

Now you can perform the following operation

a = df.apply(lambda row: row['substring_df_substring'] in row['map_df_string'], axis=1)

OUTPUT:

0    True
1    True
2    True

Now you can get the index of the series object and add plus one where the index is true to get the map_df['Number_1']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM