Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace multiple substrings of Pandas Series at once

I have a Pandas Series of golf scores with multiple substring replacements I would like to make simultaneously:

  1. Replace '+' with '' (i.e. nothing)
  2. Replace 'E' with '0'

In a dictionary, I suppose this would look like:

reps = {'+' : '', 'E' : '0'}

I have tried pandas.Series.str.replace, but that only seems to accept one argument. What I've done so far is this:

series = series.str.replace('+', '')
series = series.str.replace('E', '0')

This works, but is obviously poor form. How can I do this in one line, with any number of edits?

like image 381
Jack Overby Avatar asked Mar 06 '23 15:03

Jack Overby


1 Answers

If you are using python3 (this won't work in python2), you can use pandas.Series.str.translate as follows:

import pandas as pd
reps = {'+' : '', 'E' : '0'}
series = pd.Series(['+1', 'E', '+5', '-1'])

print(series)
#0    +1
#1     E
#2    +5
#3    -1
#dtype: object

print(series.str.translate(str.maketrans(reps)))
#0     1
#1     0
#2     5
#3    -1
#dtype: object

A better way to verify that it's doing what you expect:

print(series.str.translate(str.maketrans(reps)).values)
#array(['1', '0', '5', '-1'], dtype=object)
like image 99
pault Avatar answered Mar 08 '23 05:03

pault