I have a Pandas Series of golf scores with multiple substring replacements I would like to make simultaneously:
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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With