Skip to content
Advertisement

How to modify pandas column if value doesnt match requirements?

I am having trouble to format evenly my pandas df.

It is filled with dates and prices for Stocks, but the prices are not formatted equally.

From the start of 2021, the values have a comma separating the decimal (cents), but from 1998 to 2020, the prices are not seppareted with comma or dot.

How can I add a comma to the values where the comma doesnt show?

Sample of my df:

            Data Ticker          ISIN Mercado  ...    MAX    MED   LAST          QTD
0     16/03/1998  PETR4  BRPETRACNPR6    CASH  ...  27600  27460  27500     98610000
1     17/03/1998  PETR4  BRPETRACNPR6    CASH  ...  27800  27577  27700    148850000
2     18/03/1998  PETR4  BRPETRACNPR6    CASH  ...  27900  27728  27550    146080000
3     19/03/1998  PETR4  BRPETRACNPR6    CASH  ...  27700  27489  27550    108410000
4     20/03/1998  PETR4  BRPETRACNPR6    CASH  ...  28000  27823  27900     96010000
...          ...    ...           ...     ...  ...    ...    ...    ...          ...
5661  03/02/2021  PETR4  BRPETRACNPR6    CASH  ...  29,14  28,87  28,85   47486800.0
5662  04/02/2021  PETR4  BRPETRACNPR6    CASH  ...  28,99  28,62  28,82   41444100.0
5663  05/02/2021  PETR4  BRPETRACNPR6    CASH  ...  30,09  29,35  29,02  121098700.0
5664  08/02/2021  PETR4  BRPETRACNPR6    CASH  ...   29,3  28,38  28,11   98821700.0
5665  09/02/2021  PETR4  BRPETRACNPR6    CASH  ...  28,37  27,72  27,54   63662200.0

[5666 rows x 9 columns]

Advertisement

Answer

For one particukar column MAX. Same can be applied to required columns. You can use pandas.str.replace

In this case a string xxxxxx, it adds comma before last 2 digits like xxxx,xx

df['MAX'].str.replace(r'(d+)(d{2}$)', lambda x: x[1]+','+x[2])
0    278,00
1    279,00
2    277,00
3    280,00
4     29,14
5     28,99
6     30,09
7      29,3
8     28,37
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement