Skip to content
Advertisement

How to Extract Month Name and Year from Date column of DataFrame

I have the following DF

JavaScript

I want to extract the month name and year in a simple way in the following format:

JavaScript

I have used the df.Date.dt.to_period("M") which return "2018-01" format.

Advertisement

Answer

Cast you date from object to actual datetime and use dt to access what you need.

JavaScript

Visual Format without affecting data types

We could also work with style to get the visual in the way we want without messing with underlying types

JavaScript
Advertisement