Python 在pandas中循环并应用正则表达式
我使用以下脚本来选择数据集中的列:Python 在pandas中循环并应用正则表达式,python,pandas,loops,Python,Pandas,Loops,我使用以下脚本来选择数据集中的列: df_c = pd.DataFrame(df, columns =['Year','Month','Title','Term','date','Company','Location']) 我将以下代码分别应用于每个变量的查找和替换: df_c['Location'] = df_c['Location'].str.replace(',', '') 假设我有20个变量,如何将代码同时应用于所有变量?循环“?我相信您需要使用regex=True替换子字符串:
df_c = pd.DataFrame(df, columns =['Year','Month','Title','Term','date','Company','Location'])
我将以下代码分别应用于每个变量的查找和替换:
df_c['Location'] = df_c['Location'].str.replace(',', '')
假设我有20个变量,如何将代码同时应用于所有变量?循环“?我相信您需要使用regex=True
替换子字符串:
df_c = df_c.replace(',', '', regex=True)
如果要仅对某些列应用替换:
cols = ['Year','Month','Title','Term']
df_c[cols] = df_c[cols].replace(',', '', regex=True)