0

I am seeking ways to manipulate the last character of the string found within a specific column of a dataframe based on key-value pairs I have created in a dictionary. I need the letter character shown in the dataframe below to be changed to the value that corresponds in the dictionary (IE in line 0 the data point would change to "0000000027581"). Any input would be appreciated!

DataFrame:

Column 1
0   000000002758A
1   000000326588B
2  0000000000000{

Dictionary:

Key = {"{":"0", "A":"1", "B":"2"}
zestyfred
  • 21
  • 2
  • Here a simila problem: https://stackoverflow.com/questions/52850192/python-extract-last-digit-of-a-string-from-a-pandas-column – Glauco Dec 06 '21 at 16:11

2 Answers2

0

You can use the method of series str available only for object dtype

df['Column 1'].str[-1]

do the job

Glauco
  • 1,385
  • 2
  • 10
  • 20
0

Join everything up to the final character with the final character mapped with your dictionary.

Key = {"{":"0", "A":"1", "B":"2"}

df['Column 1'] = df['Column 1'].str[:-1] + df['Column 1'].str[-1].replace(Key)

print(df)
         Column 1
0   0000000027581
1   0000003265882
2  00000000000000
ALollz
  • 57,915
  • 7
  • 66
  • 89