Stark
Stark

Reputation: 197

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

Upvotes: 0

Views: 142

Answers (2)

Felix K Jose
Felix K Jose

Reputation: 892

You could achieve this as follows:

from datetime import datetime

import pandas as pd

df1 = pd.DataFrame({'YEAR': ['10', '11', '98', '02'], 'DWT': [6571, 6421, 7786, 9191], 'RFR': [1200, 1200, 3000, 1250]})
df1['YEAR'] = [datetime.strptime("01/01/" + yr, "%m/%d/%y").year for yr in df1['YEAR']]
print(df1)

   YEAR   DWT   RFR
0  2010  6571  1200
1  2011  6421  1200
2  1998  7786  3000
3  2002  9191  1250

Upvotes: 0

Corralien
Corralien

Reputation: 120499

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

Upvotes: 2

Related Questions