0

I was doing pandas query on sub-string as a whole word

`dfs = []
for row in mapping_excel.to_dict(orient='records'):
    query_filter = ' & '.join([f'{k}.str.contains("{v}",case= False, na=False)' for k,v in row.items() if v  and k != 'exclusion_criteria'])
    df = final.query(query_filter,engine='python')
    df['exclusion_criteria'] = row['exclusion_criteria']
    dfs.append(df)
exc_df = pd.concat(dfs)`

This is working but it working for sub strings of "{v}" and I want to filter it on whole word of "{v}" so I changed code

dfs = []
`**for row in mapping_excel.to_dict(orient='records'):
    query_filter = ' & '.join(([f'{k}.str.contains({r'\bv\b')},case=False, na=False)' for k,v in row.items() if v and k != 'exclusion_criteria'])
    df = final.query(query_filter,engine='python')
    df['exclusion_criteria'] = row['exclusion_criteria']
    dfs.append(df)
exc_df = pd.concat(dfs)** 

` but getting error File "", line 229 query_filter = ' & '.join(([f'{k}.str.contains({r'\bv\b')},case=False, na=False)' for k,v in row.items() if v and k != 'exclusion_criteria']) ^ SyntaxError: unexpected character after line continuation character

1 Answers1

0
dfs = []
for row in mapping_excel.to_dict(orient='records'):
    query_filter = ' & '.join([f'{k}.str.contains("{r'\\bv\\b'}",case= False, na=False)' for k,v in row.items() if v  and k != 'exclusion_criteria'])
    df = final.query(query_filter,engine='python')
    df['exclusion_criteria'] = row['exclusion_criteria']
    dfs.append(df)
exc_df = pd.concat(dfs)
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 14 '23 at 02:17