【发布时间】:2020-11-21 11:31:18
【问题描述】:
这是我的 df:
df = pd.DataFrame({'date':['2020-01-01 12:00:00','2020-01-01 15:00:00','2020-01-06 07:00:00','2020-01-15 13:00:00','2020-01-22 12:00:00'],
'numb_total':[8,25,11,14,8]})
df['date'] = pd.to_datetime(df['date'])
给我:
date numb_total
0 2020-01-01 12:00:00 8
1 2020-01-01 15:00:00 25
2 2020-01-06 07:00:00 11
3 2020-01-15 13:00:00 14
4 2020-01-22 12:00:00 8
现在我想添加一个新列,在特殊条件下给我numb_total * x (x=5),否则为*y (y=10)。
条件:
如果date 是“星期一”或“星期二”并且日期的time 介于08:00 - 14:00 之间:
df['numb_new'] = df['numb_total']*x
其他:
df['numb_new'] = df['numb_total']*y
为了获得day_name 和time,我这样做了:
df['day'] = df['date'].dt.day_name()
df['time'] = df['date'].dt.time
如何创建这个新专栏df['numb_new'] 高效?
【问题讨论】:
标签: python python-3.x pandas dataframe apply