Pandas str split a column and add it to a dataframe

Viewed 74

so I have a dataframe with this information and for the release date, I want to str,split so that it only display the year.

how can i str.split the release date so that it only shows the year and label it "release year"

enter image description here

2 Answers
df['release year'] = df['releaseDate'].apply(lambda x: x.split('-')[-1])

You can access the year and month attributes directly using the following:

import pandas as pd
df = pd.DataFrame({"data" : ["11-Jan-2020", "05-Feb-2020", "01-Mar-2020"]})
df["data"]=pd.to_datetime(df["data"], dayfirst=True)
df['year'] = df['data'].dt.year
df.head()

Your df will now look like:

        data    year
0   2020-01-11  2020
1   2020-02-05  2020
2   2020-03-01  2020
Related