Reformat only cells that contain

Question:

I’m trying to understand how I can find cells in a dataframe that contain a specific substring (I want to chop off the ‘R’ at the end of certain strings) and reformat those cell to leave the original value minus the last character

example:

  "Value"   "Designator"
1  47        R12
2  47R       R13
3  220K      R14
4  RGB LED   LED1
5  220R      R15
  

should become

  "Value"   "Designator"  
1  47        R12
2  47        R13
3  220K      R14
4  RGB LED   LED1
5  220       R15

I can find the cells with .contains and a regex expression but I couldn’t see how I can keep the original contents and only chop the ‘R’ off the end

any help much appreciated

Asked By: ian4339

||

Answers:

If need remove R if last value after digit use:

df['Value'] = df['Value'].str.replace('(d)R$',r'1', regex=True)
print (df)
 "Value"   "Designator"
1  47        R12
2  47R       R13
3  220K      R14
4  RGB LED   LED1
5  220R      R15

Or if need R from end of all strings:

df['Value'] = df['Value'].str.rstrip('R')
print (df)
     Value Designator
1       47        R12
2       47        R13
3     220K        R14
4  RGB LED       LED1
5      220        R15
Answered By: jezrael

apply rstrip on all the columns:

df = pd.DataFrame({"Value": ['47', '47R', '220K', 'RGB LED', '220R'],
                   "Designator": ['R12', 'R13', 'R14', 'LED1', 'R15']})

df[df.columns] = df.apply(lambda x: x.str.rstrip('R'))

print(df.head())

output:

     Value Designator
0       47        R12
1       47        R13
2     220K        R14
3  RGB LED       LED1
4      220        R15
Answered By: Ali
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.