This is my df, I want to convert column month and year into quarter. please help]
CodePudding user response:
You can create a very simple function to get quarter value based on month value and use df.apply() to calculate the quarter for each row. Here is quick sample:
def get_quarter(mon):
if mon == 'jan' or mon == 'feb' or mon == 'mar':
return 1
elif mon == 'apr' or mon == 'may' or mon == 'jun':
return 2
elif mon == 'jul' or mon == 'aug' or mon == 'sep':
return 3
else:
return 4
df['quarter'] = df['month'].apply(lambda x: get_quarter(x))
df.head()
month year quarter
0 jan 2012 1
1 apr 2016 2
2 dec 1998 4
CodePudding user response:
Since you mentioned month and year. I'm assuming you may want yyQx format. I used @Devesh Shukla code as a starting point for the code below.
import pandas as pd
cols = ["something","month","year"]
data=[ [1,'jan',2020],
[1,'feb',2020],
[2,'jun',2021],
[2,'aug',2021],
[3,'sep',2022],
[3,'nov',2022],
[3,'dec',2022] ]
df = pd.DataFrame(data, columns = cols )
def get_quarter(m):
if m == 'jan' or m == 'feb' or m == 'mar':
return 1
elif m == 'apr' or m == 'may' or m == 'jun':
return 2
elif m == 'jul' or m == 'aug' or m == 'sep':
return 3
else:
return 4
def get_year_quarter(m, y):
y = str(y)[-2:]
m = m.lower()
if m == 'jan' or m == 'feb' or m == 'mar':
q = 'Q1'
elif m == 'apr' or m == 'may' or m == 'jun':
q = 'Q2'
elif m == 'jul' or m == 'aug' or m == 'sep':
q = 'Q3'
elif m == 'oct' or m == 'nov' or m == 'dec':
q = 'Q3'
else:
q = 'XX'
return str(y) q
df['quarter'] = df['month'].apply(lambda x: get_quarter(x))
df['YrQtr'] = df.apply(lambda x: get_year_quarter(x.month, x.year), axis=1)
Output:
something month year quarter YrQtr
0 1 jan 2020 1 20Q1
1 1 feb 2020 1 20Q1
2 2 jun 2021 2 21Q2
3 2 aug 2021 3 21Q3
4 3 sep 2022 3 22Q3
5 3 nov 2022 4 22Q3
6 3 dec 2022 4 22Q3