I'm using the data and dataframe below to use a search_string to query one column/series, and then when the string is a match, update information in different column/series. I can get it done, but not how I want to - I want spaces between the text updates. I have exhausted my searching and looking at the documentation. The closest I have found is str.cat - but that only seems to work for the entire series. I repeatedly receive the "ValueError: Did you mean to supply a sep keyword?"

Below is shows what works (albeit without spaces) commented out and what currently does NOT work.

import pandas as pd
search_str = ['STRAUSS', 'STREET', 'STUBBY\'S']

data = {
  "calories": ['STRAUSS_STREET', 'ten', 'twenty'],
  "duration": [50, 40, 45],
  "test": ['not_yet_set', 'not_yet_set', 'not_yet_set']    
}

df_1 = pd.DataFrame(data)
df_1["calories"] = pd.Series(df_1["calories"], dtype=pd.StringDtype)

for k in range(len(search_str)):
    #df_1.loc[df_1['calories'].str.contains(search_str[k]), 'test'] += search_str[k]
    df_1.loc[df_1['calories'].str.contains(search_str[k]), 'test'] = 
        df_1['test'].str.cat(search_str[k], sep=',', na_rep='-')

df_1
2

There are 2 best solutions below

0
Sani On BEST ANSWER

What you have written works, you just need to edit this line by adding ", "+ to it to have the comma and space:

    df_1.loc[df_1['calories'].str.contains(search_str[k]), 'test'] +=
 ", "+search_str[k]
0
Nick On

IIUC, you can create a regex from your search terms and then use str.findall to find all matches in df_1['calories']. This will produce a list, which you can then join using map:

rgx = '|'.join(search_str)
df_1['test'] = df_1['calories'].str.findall(rgx).map(', '.join)

Output:

         calories  duration             test
0  STRAUSS_STREET        50  STRAUSS, STREET
1             ten        40
2          twenty        45