473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Replace the last letter when letter matches

Hi There,

If the last letter contains number 5 then I want to replace it with 0.

id PA005 and XP095 contain 5 at the last I want to replace 5 to 0.
In a similar fashion, if the second letter contains A then replace it with Z. ID Ba002, CA003 and Pa005 contains the second letter A and i want to replace it to Z

Below is the dataframe:
Expand|Select|Wrap|Line Numbers
  1. data = {'id': ['BP001', ' Ba002', 'CA003', 'EF004', 'Pa005','EP001','EM050','XP095']} 
  2. df = pd.DataFrame(data, columns = ['id'])
  3. df
.

Appreciate your help.
Mar 6 '20 #1

✓ answered by SioSio

If id is always 5 characters.
Expand|Select|Wrap|Line Numbers
  1. df['id'] = df['id'].str.replace(' ','')
  2. df['id'] = df['id'].str[:4] + df['id'].str[-1].replace('5', '0')
  3. df['id'] = df['id'].str[:1] + df['id'].str[1].replace('a', 'Z').replace('A', 'Z') + df['id'].str[2:]
  4.  

2 2459
SioSio
272 256MB
If id is always 5 characters.
Expand|Select|Wrap|Line Numbers
  1. df['id'] = df['id'].str.replace(' ','')
  2. df['id'] = df['id'].str[:4] + df['id'].str[-1].replace('5', '0')
  3. df['id'] = df['id'].str[:1] + df['id'].str[1].replace('a', 'Z').replace('A', 'Z') + df['id'].str[2:]
  4.  
Mar 10 '20 #2
Hi There,
Thanks for advice.
Kind regards,
Chandan
Mar 10 '20 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Lance Koh | last post by:
hi, i have a regular expression question here. string: "({WBC} * {FBC} - {ABC} * 5)" pattern: "(.*?)(\{)(.+?)(\})(.*?)" replace: "$3 " expected result: "WBC FBC ABC" returned result: "WBC...
2
by: Karlwasley | last post by:
HI I have a ata feild with a stand code EG A23H, there are three letters H, W & C but i do not wish them to be printed in the reports. How can i remove this letter during a query thatr creates a...
0
by: almurph | last post by:
Hi, Hope you can help me. This is a trick one - at least I think so. I have 2 strings. I have to calculate the "score" of the strings. Score is determined by matching patterns of letters within...
2
by: lekshminair | last post by:
dears can u help me remove last letter in a string. eg: arises in this string remove only last 's' ouput:arise
4
by: pnsreee | last post by:
Hi all, I have a string "Post.lang.tmp.txt" and i have to replase the ".txt" with "_large.txt". Please help me regarding this problem. I got it in shell scripting using `echo $string |...
2
by: shapper | last post by:
Hello, I am trying to remove the last letter from a string: names.Remove(names.Remove(names.Length - 1, 1)) I get index out of range. What am I doing wrong? Thanks, Miguel
8
by: Avi | last post by:
Hi all, I'm using string Replace(string oldValue, string newValue) and would like it to replace only full words that matches oldValue and not when oldValue is a substring of a larger word. ...
5
by: ggftw | last post by:
Hi, I have a field named CustomerNRIC and have an input mask of L0000000L. I need a code ( at OnEnter I assume ) so that when I enter the IC, the first letter must be either "S" or "T" and the last...
13
by: Yousaf Shah | last post by:
Hello everybody Thank you all for helping others. I have a very simple question that how can we replace only last comma with 'and' in string, which has got multiple commas? Suppose we have a...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.