You can use the following code to convert the month number to month name in Pandas.
Full name:
df['date'].dt.month_name()
3 letter abbreviation of the month:
df['date'].dt.month_name().str[:3]
Next, you'll see example and steps to get the month name from number:
Step 1: Read a DataFrame and convert string to a DateTime
For example, let's have a DataFrame with a date column inside which is stored a string.
DataFrame and steps are explained here - How to Search and Download Kaggle Dataset to Pandas DataFrame or if you use the notebook:
import pandas as pd
df = pd.read_csv('../data/medium_data.csv.zip')
df
In case of a date which is stored as a string you can convert it to a datetime by:
df['date'] = pd.to_datetime(df['date'])
the data is shown below:
title | date | claps |
---|---|---|
A Beginner’s Guide to Word Embedding with Gensim Word2Vec Model | 2019-05-30 | 850 |
Hands-on Graph Neural Networks with PyTorch & PyTorch Geometric | 2019-05-30 | 1100 |
How to Use ggplot2 in Python | 2019-05-30 | 767 |
Databricks: How to Save Files in CSV on Your Local Computer | 2019-05-30 | 354 |
A Step-by-Step Implementation of Gradient Descent and Backpropagation | 2019-05-30 | 211 |
Step 2: Convert Month Number to Full Month Name
There are several different ways to get a full month name in Pandas and Python.
First we will show the built in dt
method - .dt.month_name()
:
df['month_full'] = df['date'].dt.month_name()
This will return the full month name as a new column:
title | date | claps | month_full |
---|---|---|---|
How to Get and Keep Clients as a Freelancer | 2019-05-27 | 965 | May |
A Battle for the Soul of BreadTube Is Currently Taking Place | 2019-07-29 | 105 | July |
<strong class="markup--strong markup--h3-strong">Business Opportunity: The Colombian Oils and Lubricants Market</strong> | 2019-02-09 | 0 | February |
How to know your price is right | 2019-12-05 | 102 | December |
3 Types of Reports That Business Analysts Need to Learn | 2019-09-18 | 309 | September |
Step 3: Convert Month Number to Month abbreviation
In this step instead of the full month name we will get an abbreviation of the first 3 letters of the name. It's going to use the same method plus str
slicing:
df['month_short'] = df['date'].dt.month_name().str[:3]
The result are short names like:
Jul
Mar
Mar
Dec
Aug
Step 4: Convert Month Number to Month name with strftime
One more way to convert a month to it's name is by using the method - strftime
. This can be done by:
df['date'].dt.strftime('%b')
this will return short names as:
Jul
Mar
Mar
Dec
Aug
For a full month name you can use %B
:
df['date'].dt.strftime('%B')
output:
July
March
Step 5: Convert Month Number to custom name with mapping
Finally, what if you like to get custom labels or month names in different languages like Spanish or French.
Then you can apply a custom mapping with apply
:
import pandas as pd
month_labels = {1: 'I', 2: 'II', 3: 'III', 4: 'IV', 5: 'V', 6: 'VI', 7: 'VII', 8: 'VIII',
9: 'IX', 10: 'X', 11: 'XI', 12: 'XII'}
df['month'] = df['date'].dt.month
df['month'].apply(lambda x: month_labels[x])
Above you can find the mapping - month_labels
which can be updated to suit your needs.
You need also to get the month number by df['date'].dt.month
result is:
III
VII