Home > Net >  How to add a column with value based on date condition in pandas dataframe?
How to add a column with value based on date condition in pandas dataframe?

Time:01-20

I have a dataframe with the below sample columns.

I have a daily data in date column. I want to add a column with specific value based on date.

If the date is before 29 then it should include before 12/29 value to the new date range column, after 30 means it should include after 12/30 and same like others.

Date        product        result
12/27/2021  tv              6
12/27/2021  tv             38
12/27/2021  tv             2
12/28/2021  broadband      5        
12/29/2021  tv             6                
12/30/2021  broadband      7
12/30/2021  tv             10
12/31/2021  broadband      48
01/06/2022  broadband      48

Expected output



Date          day_range         product        result
12/27/2021    before 12/29      tv              6
12/27/2021    before 12/29      tv              38
12/27/2021    before 12/29      tv              2
12/28/2021    before 12/29      broadband       5       
12/29/2021    29-Dec            tv              6               
12/30/2021    30-Dec            broadband       7
12/30/2021    30-Dec            tv              10
12/31/2021    After 12/30       broadband       48
01/06/2022    After 12/30       broadband       4

Is there any approach to achieve this

CodePudding user response:

Use numpy.select:

df['Date'] = pd.to_datetime(df['Date'])

 
df['day_range'] = np.select([df['Date'].lt('2021-12-29'),  
                             df['Date'].gt('2021-12-30')], 
                            ['before 12/29','After 12/30'], 
                            default=df['Date'].dt.strftime('%d-%b'))

print (df)
        Date    product  result     day_range
0 2021-12-27         tv       6  before 12/29
1 2021-12-27         tv      38  before 12/29
2 2021-12-27         tv       2  before 12/29
3 2021-12-28  broadband       5  before 12/29
4 2021-12-29         tv       6        29-Dec
5 2021-12-30  broadband       7        30-Dec
6 2021-12-30         tv      10        30-Dec
7 2021-12-31  broadband      48   After 12/30
8 2022-01-06  broadband      48   After 12/30
  •  Tags:  
  • Related