简体   繁体   中英

Remove the same character from strings in column

I'm trying to remove a specific character in a specific position in a column. Is there any way that I can do it with the strip position type function? Any support is appreciated!! Thanks in advance!!

Sample df

Report
RPi-1
RPi-2
RPi-3
RPi-4

I would like to remove the 'i' at position 3 of the string on every row within Report column.

Desired df

Report
RP-1
RP-2
RP-3
RP-4

This works. You can replace "i" with any arbitrary charactr that you want to replace

df[col]=df[col].apply(lambda x: x.replace("i", ""))

df['Report'] = df['Report'].str.replace('RPi', 'RP')

or

df["Report"]=df["Report"].str.replace("i","")

Hope this will help you

If you want to replace a character at a particular position, you can do it using Series.str :

df.Report = df.Report.replace(df.Report.str[2], '') ## str[2] gives 3rd character

Output:

  Report
0   RP-1
1   RP-2
2   RP-3
3   RP-4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM