bkcollection
bkcollection

Reputation: 924

Python: Pandas dataframe str replace to partial lower case string

I have the pandas dataframe as below. I only want to dataframe that contains -P to _p(lowe case after that) and '-' remove and change the string after that with lower case.

>>> data= ['AAP','AAPL','BRK-A','AAIC-PB','AAP-C','YAB-PP']
>>> a = pd.DataFrame(data,columns=['code'])
>>> a
      code
0      AAP
1     AAPL
2    BRK-A
3  AAIC-PB
4    AAP-C
5   YAB-PP
>>> a['code']=a['code'].str.replace('-P','_p')
>>> a
>>> a
      code
0      AAP
1     AAPL
2    BRK-A
3  AAIC_pB
4    AAP-C
5   YAB_pP
>>> a['code']=a['code'].str.replace('-','')
>>> a
      code
0      AAP
1     AAPL
2     BRKA
3  AAIC_pB
4     AAPC
5   YAB_pP
>>> 

The desired output is

      code
0      AAP
1     AAPL
2    BRKa
3  AAIC_pb
4    AAPc
5   YAB_pp

Upvotes: 1

Views: 444

Answers (1)

Dani Mesejo
Dani Mesejo

Reputation: 61910

You could do:

import pandas as pd

data= ['AAP','AAPL','BRK-A','AAIC-PB','AAP-C','YAB-PP']
a = pd.DataFrame(data,columns=['code'])


a['code'] = a['code'].str.replace('-(P.*)', lambda x: f'_{x.group(1).lower()}')
a['code'] = a['code'].str.replace('-(\w.*)', lambda x: x.group(1).lower())
print(a)

Output

      code
0      AAP
1     AAPL
2     BRKa
3  AAIC_pb
4     AAPc
5   YAB_pp

UPDATE

In versions previous to Python 3.6:

a['code'] = a['code'].str.replace('-(P.*)', lambda x: '_{}'.format(x.group(1).lower()))
a['code'] = a['code'].str.replace('-(\w.*)', lambda x: x.group(1).lower())
print(a)

Upvotes: 2

Related Questions