cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

How to add Columns for Automatic Fill on Pandas Python

omsas
New Contributor

1. I have data x,I would like to create a new column with the condition that the value are 1, 2 or 3

2. The name of the column is SHIFT where this SHIFT column will be filled automatically if the TIME_CREATED column meets the conditions.

3. the condition are :

# 00:00:00 - 07.59.59 = Shift 1

# 08:00:00 - 15:59:59 = Shift 2

# 16:00:00 - 23:59:59 = Shift 3

4. on the above conditions apply only to Time as hours:minutes:seconds

5. I attach the link of the py and ipynb files for test and study

6. I've tried the test only for the selection using a query but it's still stuck and it only shows up for 1 day.

https://drive.google.com/drive/folders/1xdDwNfbDNzYb2SfJ_h-DurY_KXFAj9rl?usp=sharing

Columns TableResult of testedThanks, 

2 REPLIES 2

-werners-
Esteemed Contributor III

seems like a simple case statement, or elif in python, no?

Ryan_Chynoweth
Honored Contributor III

You an do something like this in pandas. Note there could be a more performant way to do this too.

import pandas as pd
import numpy as np
 
df = pd.DataFrame({'a':[1,2,3,4]})
df.head()
>      a
> 0  1
> 1  2
> 2  3
> 3  4
 
conditions = [(df['a'] <=2), (df['a']>2)]
values = ['value1', 'value2']
 
 
df['b'] = np.select(conditions, values)
 
df.head()
         a       b
> 0  1  value1
> 1  2  value1
> 2  3  value2
> 3  4  value2

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.