1

I'm trying to modify the string values in a dataframe column. So far I have tried this:

def years(x):
    x = x.split(' ')[0]
    x = x[-2:]
    return x

df['Date'].apply(years)

When I run this and then take a look at the dataframe the actual df['Date'] column is unmodified.

For reference, I'm trying to change the column values from a string that looks like this, '12/21/15 20:30', into a string (or integer) that just includes the year, so in this case, '15'.

1 Answer 1

4

You need to assign the returned value to a new column. .apply does not operate in-place.

def years(x):
x = x.split(' ')[0]
x = x[-2:]
return x

df['NewDate'] = df['Date'].apply(years)
Sign up to request clarification or add additional context in comments.

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.