Create two new columns derived from original columns in Python

2024/10/6 19:22:18

I have a dataframe, df, that contains two columns that contain quarter values. I would like to create two more columns that are the equivalent "long dates".

Data

ID  Quarter Delivery    
A   Q1 2022 Q3 2022 
A   Q1 2022 Q3 2022 
B   Q1 2022 Q3 2022 
B   Q1 2022 Q3 2022 

Desired

ID  Quarter Delivery    QuarterFull DeliveryFull    
A   Q1 2022 Q3 2022     1/1/2022    07/1/2022   
A   Q1 2022 Q3 2022     1/1/2022    07/1/2022   
B   Q4 2022 Q2 2023     10/1/2022   04/1/2023   
B   Q4 2022 Q2 2023     10/1/2022   04/1/2023   Q1 is 01
Q2 is 04
Q3 is 07
Q4 is 10

Doing

My approach/logic is to create variables or a dictionary that holds or map to specific dates (key/value pairs)

dict = {"Q1": "01","Q2": "04","Q3": "07""Q4": "10"
}

Not exactly sure how to implement this as columns. I am still troubleshooting this. Any suggestion is appreciated

Answer

Code

import pandas as pddf = pd.DataFrame({'ID': ['A', 'B'], 'Quarter':['Q1 2020', 'Q2 2021']})def convDate(date):q, year = date.split()q = int(q[1])return f'{q*3-2:02d}/1/{year}'df['QuarterFull'] = df['Quarter'].map(convDate)
df.head()

Output

   ID   Quarter QuarterFull
0   A   Q1 2020 01/1/2020
1   B   Q2 2021 04/1/2021
https://en.xdnf.cn/q/118915.html

Related Q&A

In dataframe: how to pull minutes and seconds combinedly(mm:ss) from timedelta using python

i have already tried these but by this we can only get hour/minute/second but not both minute and second In[7]: df[B] = df[A].dt.components[hours] df Out[7]:A B0 02:00:00 2 1 01:00:00 1from this tim…

Understanding python numpy syntax to mask and filter array

Please help me understand how the lines below are working.How does a pair of parentheses create an array and then individual elements go through logical condition check and create a new array? How doe…

How to get the proper link from a website using python beautifulsoup?

When I try to scrape roster links, I get https://gwsports.com/roster.aspx?path=wpolo when I open it on chrome it changes to https://gwsports.com/sports/mens-water-polo/roster. I want to scrape it in p…

tkinter frame propagate not behaving?

If you uncomment the options_frame_title you will see that it does not behave properly. Am I missing something? That section was just copied and pasted from the preview_frame_title and that seems to h…

python modules installing Error Visual c++ 14.0 is required [duplicate]

This question already has answers here:pip install ecos errors with "Microsoft Visual C++ 14.0 is required." [duplicate](1 answer)Error "Microsoft Visual C++ 14.0 is required (Unable to …

How to render Flask Web App with Javascript [duplicate]

This question already has answers here:Return JSON response from Flask view(15 answers)How to append ajax html response to next to current div(5 answers)Closed 5 years ago.Edit: Hi Ive checked the dupl…

Use start and stop function with same button in Tkinter

With the help of the command button, I am able to disconnect the frame in Tkinter. But is there any way which helps to use the same button to start also?import tkinter as tk counter = 0 def counter_la…

How to restrict my students to dont access teacher area in django? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…

invert edge values in python boolean list

I have a list of booleans likel = [False, False, False, True, True, True, False, False, True, False, False]and want to invert every edge value that is False like[True, True, True, True, True, True, Fal…

Unable to write text on mouseclick area on Image

I am trying to draw text on Image where the user clicks. Getting this error:Exception in Tkinter callback Traceback (most recent call last):File "C:\Users\Admin\AppData\Local\Programs\Python\Pytho…