Python 3.x python3pandas';数据帧
我正在继续学习我的熊猫技能,遇到了一些难题。这个问题涉及两个数据帧df1和df2。df1包含事件时间和每个事件的相应详细信息。df2包含由开始时间和停止时间确定的时间段 目标:Python 3.x python3pandas';数据帧,python-3.x,pandas,dataframe,pandas-groupby,python-datetime,Python 3.x,Pandas,Dataframe,Pandas Groupby,Python Datetime,我正在继续学习我的熊猫技能,遇到了一些难题。这个问题涉及两个数据帧df1和df2。df1包含事件时间和每个事件的相应详细信息。df2包含由开始时间和停止时间确定的时间段 目标: 按时间段将所有事件分组 检查计数是否在该时间段内增加,以及该时间段的所有代码是否相同 在df2中创建一个新列,如果第2部分中的两个部分都是True,则返回True到该列;如果不是True,则返回False,或者在该期间没有事件 代码: 创建df2代码: df2 = {'Start Time' : ['2020-12-01
df2 = {'Start Time' : ['2020-12-01 00:00:00', '2020-12-01 00:30:00','2020-12-01 01:30:00',
'2020-12-01 02:30:00', '2020-12-01 03:30:00' , '2020-12-01 04:30:00' ,
'2020-12-01 05:30:00' , '2020-12-01 07:30:00' , '2020-12-01 10:30:00' ,
'2020-12-01 15:00:00' , '2020-12-02 21:00:00'] ,
'End Time' : ['2020-12-01 00:30:00', '2020-12-01 01:30:00','2020-12-01 02:30:00',
'2020-12-01 03:30:00', '2020-12-01 04:30:00' , '2020-12-01 05:30:00' ,
'2020-12-01 07:30:00' , '2020-12-01 10:30:00' , '2020-12-01 15:00:00' ,
'2020-12-01 21:00:00' , '2020-12-02 00:00:00']
}
df2 = pd.DataFrame(df2 , columns = ['Start Time' , 'End Time'])
df2['Start Time'] = pd.to_datetime(df2['Start Time'])
df2['End Time'] = pd.to_datetime(df2['End Time'])
df2
Start Time End Time
0 2020-12-01 00:00:00 2020-12-01 00:30:00
1 2020-12-01 00:30:00 2020-12-01 01:30:00
2 2020-12-01 01:30:00 2020-12-01 02:30:00
3 2020-12-01 02:30:00 2020-12-01 03:30:00
4 2020-12-01 03:30:00 2020-12-01 04:30:00
5 2020-12-01 04:30:00 2020-12-01 05:30:00
6 2020-12-01 05:30:00 2020-12-01 07:30:00
7 2020-12-01 07:30:00 2020-12-01 10:30:00
8 2020-12-01 10:30:00 2020-12-01 15:00:00
9 2020-12-01 15:00:00 2020-12-01 21:00:00
10 2020-12-01 21:00:00 2020-12-02 00:00:00
战略:
我的策略是先使用lambda函数,然后使用lambda函数进行条件语句检查,但我似乎无法使它工作
以下是所需的输出:
Start Time End Time Test
0 2020-12-01 00:00:00 2020-12-01 00:30:00 True
1 2020-12-01 00:30:00 2020-12-01 01:30:00 False
2 2020-12-01 01:30:00 2020-12-01 02:30:00 True
3 2020-12-01 02:30:00 2020-12-01 03:30:00 False
4 2020-12-01 03:30:00 2020-12-01 04:30:00 True
5 2020-12-01 04:30:00 2020-12-01 05:30:00 True
6 2020-12-01 05:30:00 2020-12-01 07:30:00 True
7 2020-12-01 07:30:00 2020-12-01 10:30:00 True
8 2020-12-01 10:30:00 2020-12-01 15:00:00 False
9 2020-12-01 15:00:00 2020-12-01 21:00:00 False
10 2020-12-02 21:00:00 2020-12-02 00:00:00 False
可以使用基于指定条件生成布尔值的自定义函数。在这里,我们用来检查递增中的
计数
,并检查给定时间段内的所有代码是否相同:
def f():
for x, y in df2.to_numpy():
s = df1[df1['Event'].between(x, y)]
yield s['Count'].is_monotonic_increasing & s['Code'].nunique() == 1
df2['Test'] = list(f())
结果:
Start Time End Time Test
0 2020-12-01 00:00:00 2020-12-01 00:30:00 True
1 2020-12-01 00:30:00 2020-12-01 01:30:00 False
2 2020-12-01 01:30:00 2020-12-01 02:30:00 True
3 2020-12-01 02:30:00 2020-12-01 03:30:00 False
4 2020-12-01 03:30:00 2020-12-01 04:30:00 True
5 2020-12-01 04:30:00 2020-12-01 05:30:00 True
6 2020-12-01 05:30:00 2020-12-01 07:30:00 True
7 2020-12-01 07:30:00 2020-12-01 10:30:00 True
8 2020-12-01 10:30:00 2020-12-01 15:00:00 False
9 2020-12-01 15:00:00 2020-12-01 21:00:00 False
10 2020-12-02 21:00:00 2020-12-02 00:00:00 False
“似乎无法使其工作”,您能否分享您迄今为止尝试过的代码,以及该代码存在的问题。我尝试了许多不同的方法,但一般策略是在两次之间使用,然后两者都使用。在这个一般结构中应用:df['new column name']=df['column name']。apply(lambda x:‘满足条件时的值’if x condition else‘不满足条件时的值’)但这似乎只适用于一种情况。@kdbaseball8我猜索引为
2
的行的输出应该是True
,而索引为10
的行的输出应该是False
不,该事件只是无序,但应该应用于正确的时间间隔。如果您是正确的,我会更改它,这是一个条目错误或者谢谢你,嘘!这很有效,与我认为应该解决问题的方式完全不同。@kdbaseball8快乐编码!
Start Time End Time Test
0 2020-12-01 00:00:00 2020-12-01 00:30:00 True
1 2020-12-01 00:30:00 2020-12-01 01:30:00 False
2 2020-12-01 01:30:00 2020-12-01 02:30:00 True
3 2020-12-01 02:30:00 2020-12-01 03:30:00 False
4 2020-12-01 03:30:00 2020-12-01 04:30:00 True
5 2020-12-01 04:30:00 2020-12-01 05:30:00 True
6 2020-12-01 05:30:00 2020-12-01 07:30:00 True
7 2020-12-01 07:30:00 2020-12-01 10:30:00 True
8 2020-12-01 10:30:00 2020-12-01 15:00:00 False
9 2020-12-01 15:00:00 2020-12-01 21:00:00 False
10 2020-12-02 21:00:00 2020-12-02 00:00:00 False