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"}
CodePudding user response:
You can use the method of series str available only for object dtype
df['Column 1'].str[-1]
do the job
CodePudding user response:
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