How to replace the last entry in each line of the dataframe, the line has the form ADVA.SFP / 2G5U / D1549.32U / SM / LC # D20.SFP. You need to replace the first '.' and the last '.' . Point in the center must be left. Replaced the first point with '$' -

 import pandas as pd file = pd.read_excel('Stock_base.xlsx', sep=';', encoding='cp1251') file['Pos'] = file['pos'].str.replace('.', '$', 1) 
  • Specify in the question the line you want to get at the output - MaxU
  • one
    Thanks a lot, both methods work. The method with regular expression is difficult for me. while I understand them badly. - Vlad Golovin

2 answers 2

In order to get rid of the point at the end, you can first use the rsplit() method with the number of divisions limited to 1. And then join with join() using the separator you need.

 file['Pos'] = file['pos'].str.replace('.', '$', 1).str.rsplit('.', 1).str.join('$') 

    Example:

     In [13]: df Out[13]: Pos 0 ADVA.SFP/2G5U/D1549.32U/SM/LC#D20.SFP 1 aaa.bbb.ccc.ddd.eee In [14]: df['Pos'] = df['Pos'].str.replace(r'^([^.]*)\.(.*)\.([^.]*)', r'\1$\2$\3') In [15]: df Out[15]: Pos 0 ADVA$SFP/2G5U/D1549.32U/SM/LC#D20$SFP 1 aaa$bbb.ccc.ddd$eee 

    PS parsing Regular Expression