Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pandas中循环并应用正则表达式_Python_Pandas_Loops - Fatal编程技术网

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)