sahel
sahel

Reputation: 211

how to check exist in a string an element from another string and replace it in python

I have the following DataFrme

     Account    Buy/Sell    Amount  month
1    1001       Sell        52792   2021-Automation-Aug-Dummy.xlsx
5    3001       Buy         85802   2021-Automation-Aug-Dummy.xlsx
8    5601       Buy         10425   2021-Automation-Aug-Dummy.xlsx
11   2001       Buy         12526   2021-Automation-Aug-Dummy.xlsx
14   98071      Sell        90517   2021-Automation-Aug-Dummy.xlsx
... ... ... ... ... ... ...

I want to replace the abv name of the month instead of the long name in the 'month' column. For example '2021-Automation-Aug-Dummy.xlsx' should be replaced with 'Aug'.

I have written the following code, but it cannot replace the abv.

month_abv = ['Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov']
for inx in test.index:
    if any(abv in month_abv for abv in test.loc[inx,'month']):
        test.loc[inx,'month']= abv

But it does not change the dataframe.

Upvotes: 1

Views: 39

Answers (1)

jezrael
jezrael

Reputation: 863301

Use Series.str.extract by joined month_abv with | for regex or:

test['month'] = test['month'].str.extract(f'({"|".join(month_abv)})', expand=False)

print (test)
    Account Buy/Sell  Amount month
1      1001     Sell   52792   Aug
5      3001      Buy   85802   Aug
8      5601      Buy   10425   Aug
11     2001      Buy   12526   Aug
14    98071     Sell   90517   Aug

Or if possible get third value aftr split by - use Series.str.split:

test['month'] = test['month'].str.split('-').str[2]

print (test)
    Account Buy/Sell  Amount month
1      1001     Sell   52792   Aug
5      3001      Buy   85802   Aug
8      5601      Buy   10425   Aug
11     2001      Buy   12526   Aug
14    98071     Sell   90517   Aug

Your solution should be change with lambda function with next+iter trick for assign None if no match:

test['month']= test['month'].apply(lambda x: next(iter(abv for abv in month_abv if abv in x), None))

Upvotes: 1

Related Questions