Reputation: 568
In my pandas data frame I have a column DM with some data, I want to strip only 2 letters from that one.
DM
OLDE-T
OLAT-T
OLFR-T
OLPL-T
OLNL-T
OLBE-T
OLES-T
OLCH-T
OLIT-T
OLROET
OLDE-T
my desired solution is
DE
AT
FR
PL
NL
BE
ES
CH
IT
ROE
DE
basically, I want to remove OL
from the beginning and -T
from the ending.
one catch is that when the value is OLROET
then we don't have -T
in the end, here we have only T
.
Thanks for the help in advance!
Upvotes: 0
Views: 54
Reputation: 862501
Use Series.replace
with ^
for start of string and $
for end of string with [-]*
for possible value -
before T
:
df['DM'] = df['DM'].replace(['^OL', '[-]*T$'], '', regex=True)
print (df)
DM
0 DE
1 AT
2 FR
3 PL
4 NL
5 BE
6 ES
7 CH
8 IT
9 ROE
10 DE
Upvotes: 4