简体   繁体   中英

Strip the last character from a string if it is a letter in python dataframe

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.

You could use str.replace here:

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

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

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