我有以下脚本来选择数据集中的列:
df_c = pd.DataFrame(df, columns =['Year','Month','Title','Term','date','Company','Location'])
我将以下代码分别应用于每个变量的查找和替换:
df_c['Location'] = df_c['Location'].str.replace(',', '')
假设我有 20 个变量,我该怎么做才能同时将代码应用于所有变量?循环“?
请您参考如下方法:
我相信你需要replace使用 regex=True 替换子字符串:
df_c = df_c.replace(',', '', regex=True)
如果只想对某些列应用替换:
cols = ['Year','Month','Title','Term']
df_c[cols] = df_c[cols].replace(',', '', regex=True)
