Ian_De_Oliveira
Ian_De_Oliveira

Reputation: 291

Looping in pandas and apply regular expressions

I have the following script to selct the columns in my data set :

 df_c = pd.DataFrame(df, columns =['Year','Month','Title','Term','date','Company','Location'])

Im applying the following code as a find and replace to each variable separately:

 df_c['Location'] = df_c['Location'].str.replace(',', '')

Lets say I have 20 variables how would I do to apply the code to all variables at same time? looping "?

Upvotes: 2

Views: 75

Answers (1)

jezrael
jezrael

Reputation: 862601

I believe you need replace with regex=True for replace substrings:

df_c = df_c.replace(',', '', regex=True)

If want apply replace only to some columns:

cols = ['Year','Month','Title','Term']

df_c[cols] = df_c[cols].replace(',', '', regex=True)

Upvotes: 1

Related Questions