Given:
From below df,
df = pd.DataFrame(
{"date":['2016-6-1', '2016-9-22', '2016-10-28', '2016-11-4', '2016-6-29', '2016-10-1', '2016-6-15', '2016-7-29', '2016-11-1'],
"score":[9, 8, 8, 10, 6, 7, 7, 7, 6]
})
Perform below task:
for dates meet below criteria, add certain value to newly added column called 'staffNumber':
IF 'date' falls under 6/1/2016~9/22/2016 THAN create a new column with the value of 1.
IF 'date' falls under 9/23/2016~10/28/2016 THAN create a new column with the value of 2.
IF 'date' falls under 10/29/2016~11/4/2016 THAN create a new column with the value of 3
End-result will look like this:
df2 = pd.DataFrame(
{"date":['2016-6-1', '2016-9-22', '2016-10-28', '2016-11-4', '2016-6-29', '2016-10-1', '2016-6-15', '2016-7-29', '2016-11-1'],
"score":[9, 8, 8, 10, 6, 7, 7, 7, 6],
"staffNumber":[1,1,2,3,1,2,1,1,3]
})
What I've tried:
I usually try something before I ask any question. However, for this one I couldn't think of any approach.
I looked at using np.where & .isin from following links: 1. Python numpy where function with datetime 2. Using 'isin' on a date in a pandas column 3. Pandas conditional creation of a series/dataframe column
Any help will be appreciated!