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
What you have written works, you just need to edit this line by adding ", "+ to it to have the comma and space: