Search code examples
pythonpandasdataframeprefix

How to add a prefix string in the values of dataframe


Here I'm having a dataframe which shows only the last two digits of year:

YEAR  DWT   RFR
10    6571  1200
11    6421  1200
98    7786  3000
02    9191  1250

I want to make them as a complete year value, is there any solution for this?

expected output:

YEAR    DWT   RFR
2010    6571  1200
2011    6421  1200
1998    7786  3000
2002    9191  1250

Solution

  • Is it what you expect? The current year can be the pivot.

    >>> pivot_year = pd.Timestamp.now().year % 100
    
    >>> df['YEAR'].astype(int) \
                  .apply(lambda y: y + (2000 if y <= pivot_year else 1900))
    
    0    2010
    1    2011
    2    1998
    3    2002
    Name: YEAR, dtype: int64