0

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
3
  • YEAR is int or object(string)? Commented Jul 8, 2021 at 20:34
  • 1
    How do you expect to determine whether it's a 2000s value or a 1900s value? Commented Jul 8, 2021 at 20:35
  • Please repeat on topic and how to ask from the intro tour. “Show me how to solve this coding problem” is not a Stack Overflow issue. We expect you to make an honest attempt, and then ask a specific question about your algorithm or technique. Stack Overflow is not intended to replace existing documentation and tutorials. Commented Jul 8, 2021 at 20:36

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.