2

I have a DataFrame like so

             Year        Player
46  Jan. 17, 1971  Chuck Howley
47  Jan. 11, 1970    Len Dawson
48  Jan. 12, 1969    Joe Namath
49  Jan. 14, 1968    Bart Starr
50  Jan. 15, 1967    Bart Starr

and I only want the year to populate df_MVPs['Year']. My current method is

df_MVPs['Year'] = df_MVPs['Year'].str.replace(df_MVPs['Year'][:7], '')

but this causes an error to occur. Is there a way to do this more simply?

EDIT: I want my DataFrame to look like:

    Year        Player
46  1971  Chuck Howley
47  1970    Len Dawson
48  1969    Joe Namath
49  1968    Bart Starr
50  1967    Bart Starr
0

3 Answers 3

5

Aw man, convert to a datetime then get the year:

df_MVPs['Year'] = pd.to_datetime(df_MVPs['Year'], format='%b. %d, %Y').dt.year
Sign up to request clarification or add additional context in comments.

Comments

1

You could take the last four characters of the string:

df_MVPs['Year'] = df_MVPs['Year'].str[-4:]

>>> df_MVPs
    Year        Player
46  1971  Chuck Howley
47  1970    Len Dawson
48  1969    Joe Namath
49  1968    Bart Starr
50  1967    Bart Starr

Comments

0

I'd use .str.extract() method instead:

In [10]: df
Out[10]:
             Year        Player
46  Jan. 17, 1971  Chuck Howley
47  Jan. 11, 1970    Len Dawson
48  Jan. 12, 1969    Joe Namath
49  Jan. 14, 1968    Bart Starr
50  Jan. 15, 1967    Bart Starr

In [11]: df.Year.str.extract('.*(\d{4})$', expand=True)
Out[11]:
       0
46  1971
47  1970
48  1969
49  1968
50  1967

but you can also use .str.replace():

In [13]: df.Year.str.replace('.*(\d{4})$', r'\1')
Out[13]:
46    1971
47    1970
48    1969
49    1968
50    1967
Name: Year, dtype: object

Here is a link which explains the .*(\d{4})$ RegEx (Regular Expresiion)

2 Comments

can you point to where I can learn what "'.*(\d{4})$'," means? I am new to pandas/python
@Mark, i've added a link to regex101.com which parses and explains the RegEx expressions...

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.