d_hsp={"1":"I","2":"II","3":"III","4":"IV","5":"V","6":"VI","7":"VII","8":"VIII",
"9":"IX","10":"X","11":"XI","12":"XII","13":"XIII","14":"XIV","15":"XV",
"16":"XVI","17":"XVII","18":"XVIII","19":"XIX","20":"XX","21":"XXI",
"22":"XXII","23":"XXIII","24":"XXIV","25":"XXV"}
HSP_OLD['tryl'] = HSP_OLD['tryl'].replace(d_hsp, regex=True)
HSP_OLD
is a dataframe, tryl
is one column of HSP_OLD
, and here's some example of values in tryl
:
SAF/HSP: Secondary diagnosis E code 1
SAF/HSP: Secondary diagnosis E code 11
I use a dictionary to replace, it works for 1-10, but for 11, it will become "II" , for 12, it will become "III".
Sorry, didn't notice that you're not merely updating the field but you actually want to replace a number at the end, but even if that's the case - it's much better to properly convert your number to roman numerals than to map every possible occurrence of such (what would happen with your code if there is a number larger than 25?). So, here's one way to do it:
So now if we create your data frame as:
We can noe easily apply our function over the whole column with:
Which results in:
Of course, you can adapt the
romanize()
function to your needs to search any number within your string and turn it to roman numerals - this is just an example for how to quickly find the number at the end of the string.You need to keep the order of the items, and start searching with the longest substring.
You may use an
OrderDict
here. To initialize it, use a list of tuples. You may reverse it already here, when initializing, but you can do it later, too.