I want to convert a column that has alphanumeric values to digits
0 newyork2510 2
1 boston76w2 1
2 chicago785dw 1
3 san891dwn39210114 1
4 f2391rpg 1
so that
0 newyork2510 2
should look like
0 14523251518112510 2
similarly, rest of the whole second column.
I can only do the following which only helps me converting alphabets to digits
for character in input:
number = ord(character) - 96
output.append(number)
Can you help me?
CodePudding user response:
Try replace
with regex=True
:
# map the char to string integers
char_map = {chr(i): str(j) for j,i in enumerate(range(ord('a'), ord('z') 1), 1)}
# apply the mapping to the column
df['col1'] = df['col1'].replace(char_map, regex=True)
Output:
col0 col1 col2
0 0 14523251518112510 2
1 1 2151920151476232 1
2 2 38931715785423 1
3 3 191148914231439210114 1
4 4 6239118167 1