Home > database >  Reformat only cells that contain
Reformat only cells that contain

Time:01-11

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

CodePudding user response:

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

CodePudding user response:

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
  • Related