Home > Back-end >  Strip the last character from a string if it is a letter in python dataframe
Strip the last character from a string if it is a letter in python dataframe

Time:09-29

It is possibly done with regular expressions, which I am not very strong at.

My dataframe is like this:

import pandas as pd
import regex as re

data = {'postcode': ['DG14','EC3M','BN45','M2','WC2A','W1C','PE35'], 'total':[44, 54,56, 78,87,35,36]}

df = pd.DataFrame(data)
df

    postcode    total
0   DG14        44
1   EC3M        54
2   BN45        56
3   M2          78
4   WC2A        87
5   W1C         35
6   PE35        36

I want to get these strings in my column with the last letter stripped like so:

    postcode    total
0   DG14        44
1   EC3         54
2   BN45        56
3   M2          78
4   WC2         87
5   W1C         35
6   PE35        36

Probably something using re.sub('', '\D')?

Thank you.

CodePudding user response:

You could use str.replace here:

df["postcode"] = df["postcode"].str.replace(r'[A-Za-z]$', '')

CodePudding user response:

One of the approaches:

import pandas as pd
import re

data = {'postcode': ['DG14','EC3M','BN45','M2','WC2A','W1C','PE35'], 'total':[44, 54,56, 78,87,35,36]}

data['postcode'] = [re.sub(r'[a-zA-Z]$', '', item) for item in data['postcode']]
df = pd.DataFrame(data)
print(df)

Output:

postcode  total
0     DG14     44
1      EC3     54
2     BN45     56
3       M2     78
4      WC2     87
5       W1     35
6     PE35     36
  • Related