extract only year from date python 1

extract only year from date python

df['year'] = pd.DatetimeIndex(df['date']).year

Here is what the above code is Doing:
1. We’re creating a new column called ‘year’
2. We’re using the pd.DatetimeIndex() function to extract the year from the ‘date’ column
3. We’re assigning the year values to the ‘year’ column

Similar Posts