Python calculate a new column based on Date column

Alexander Johansson

Got a Date column with values like this : "1997-08-05T00:00:00" going from 1997 to 2017 with values for 5 working days of the week.

I want to create a new column in the dataset that calculates the quarter of the date. For example:

1997-01-01 to 1997-03-31 should become Q1
1997-04-01 to 1997-06-30 should become Q2
1997-07-01 to 1997-09-30 should become Q3
1997-10-01 to 1997-12-31 should become Q4

Not only 1997 but all the dates until 2017 should get a value on the new column called "quarter". Would be great if regardless of year, dates between 01-01 to 03-31 was given Q1, and so forth

(the dates all looks like this: 1997-08-05T00:00:00 but I dont need the part with T00:00:00)

vielkind

As long as your Date column is in a datetime format you can use the pandas to_period function.

df['quarter'] = df['Date'].dt.to_period('Q')

which will return the quarter in a format similar to 2017Q3. You can then strip out the year by:

df['quarter'] = df['quarter'].apply(lambda x: str(x)[-2:])

which will give you a column of the Q1, Q2, Q3, Q4 values.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python : Add a new column with date based on existing column in dataframe

python pandas calculate number of days based on Date column

Python Calculate New Date Based on Date Range

Split a date column, and calculate an amount based on the result

calculate new column values based on conditions in pandas

Calculate new column in pandas based on certain criteria

How to calculate a new column based on highest number in another column in R?

New column based on existing string column in Python

Python - New Column based on condition of other Column

How to insert date into new column based on initial date column

Making new column based on date with variable year

create a new column based on multiple date entries

Create a new column in a dataframe based on a date

mutate new column based on date interval

Python calculate the number of year in date column

Creating new column based on earliest date value in other column in R

Taking each element in a column to calculate and create a new column using python

calculate value in new column

Calculate count of a column based on other column in python dataframe

calculate combinations based on column?

Python Pandas Date time: Adding a new column to dataframe based on date time index conditions

pandas calculate date difference in a new column referencing previous cell

Calculate a date column in a Google Spreadsheet based on other columns

How to calculate difference in DATE based on status of another column?

How to calculate change in a column based on date across groups?

Pandas DataFrame, how to calculate a new column element based on multiple rows

In R, calculate a new column based on difference between rows

Calculate new column in pandas dataframe based only on grouped records

Pandas: How to calculate new column based on index or groupID?

TOP Ranking

HotTag

Archive