Issue
ID | Date | dummy |
---|---|---|
0 | 2019-01-03 20:00:00 | |
1 | 2019-01-04 14:30:00 | x |
2 | 2019-01-04 16:00:00 | x |
3 | 2019-01-04 20:00:00 | x |
wanted to know how would I be able to insert "X" into the dummy column based on date selection. For example I would like to input an "X" starting 2019-01-04 14:30:00 to 2019-01-04 20:00:00
I know I could just select df\["dummy"\]\[1:3\] = "X"
to accomplish this but not too sure how to grab from the dates column.
Solution
Use between
to create a boolean Series for dates between your boundaries, and np.where
to select between 'x'
and ''
:
df['dummy'] = np.where(df['Date'].between('2019-01-04 14:30:00',
'2019-01-04 20:00:00'),
'x', '')
Or, with boolean indexing:
df.loc[df['Date'].between('2019-01-04 14:30:00', '2019-01-04 20:00:00'),
'dummy'] = 'x'
Output:
ID Date dummy
0 0 2019-01-03 20:00:00
1 1 2019-01-04 14:30:00 x
2 2 2019-01-04 16:00:00 x
3 3 2019-01-04 20:00:00 x
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.