interval in dataframe to start from the first row [python 3.6.0](数据帧中从第一行开始的间隔[python 3.6.0])
                            本文介绍了数据帧中从第一行开始的间隔[python 3.6.0]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
                        
                        问题描述
以下数据的时间间隔为5分钟,正在尝试将其分组为10分钟
数据帧名称为df:
| script_id | DATE_TIME | 打开 | 高 | 低 | 关闭 | 音量 | 
|---|---|---|---|---|---|---|
| 201 | 2019-01-01 10:45:00 | 1492.9 | 1493.85 | 1492.15 | 1492.9 | 7189 | 
| 201 | 2019-01-01 10:50:00 | 1492.9 | 1495.95 | 1492.2 | 1495.85 | 15440 | 
| 201 | 2019-01-01 10:55:00 | 1495.85 | 1495.95 | 1494 | 1494.5 | 8360 | 
| 201 | 2019-01-01 11:00:00 | 1494.5 | 1494.5 | 1492 | 1492.05 | 9910 | 
| 201 | 2019-01-01 11:05:00 | 1492.05 | 1493.9 | 1492 | 1493.35 | 14961 | 
| 201 | 2019-01-01 11:10:00 | 1493.4 | 1493.4 | 1488 | 1489.25 | 16493 | 
| 201 | 2019-01-01 11:15:00 | 1489.25 | 1492 | 1489.25 | 1490.6 | 14590 | 
| 201 | 2019-01-01 11:20:00 | 1490.6 | 1491.65 | 1490 | 1491.5 | 3470 | 
执行以下代码时:
df_f = df.groupby(['script_id', pd.Grouper(key='date_time', freq=f'{tf}T')])
                            .agg(open=pd.NamedAgg(column='open', aggfunc='first'),
                                high=pd.NamedAgg(column='high', aggfunc='max'),
                                low=pd.NamedAgg(column='low', aggfunc='min'),
                                close=pd.NamedAgg(column='close', aggfunc='last'),
                                volume=pd.NamedAgg(column='volume', aggfunc='sum'))
                                .reset_index()
                print(df_f)
结果为(已从此处删除不需要的详细信息):
| DATE_TIME | 
|---|
| 2019-01-01 10:40:00 | 
| 2019-01-01 10:50:00 | 
| 2019-01-01 11:00:00 | 
| 2019-01-01 11:10:00 | 
但应该是(已从此处删除不需要的详细信息):-(预期结果)
| DATE_TIME | 
|---|
| 2019-01-01 10:45:00 | 
| 2019-01-01 10:55:00 | 
| 2019-01-01 11:05:00 | 
| 2019-01-01 11:15:00 | 
推荐答案
在调用pd.Grouper(... offset="5T")
df_f = df.groupby(['script_id', pd.Grouper(key='date_time', freq='10T', offset="5T")])
                            .agg(open=pd.NamedAgg(column='open', aggfunc='first'),
                                high=pd.NamedAgg(column='high', aggfunc='max'),
                                low=pd.NamedAgg(column='low', aggfunc='min'),
                                close=pd.NamedAgg(column='close', aggfunc='last'),
                                volume=pd.NamedAgg(column='volume', aggfunc='sum'))
                                .reset_index()
print(df_f)
   script_id           date_time     open     high      low    close  volume
0        201 2019-01-01 10:45:00  1492.90  1495.95  1492.15  1495.85   22629
1        201 2019-01-01 10:55:00  1495.85  1495.95  1492.00  1492.05   18270
2        201 2019-01-01 11:05:00  1492.05  1493.90  1488.00  1489.25   31454
3        201 2019-01-01 11:15:00  1489.25  1492.00  1489.25  1491.50   18060
旧版本的
pandas.Grouper对象使用base而不是offset。pd.Grouper(..., base=5)
>>> df_f = df.groupby(['script_id', pd.Grouper(key='date_time', freq=f'10T', base=5)])
                            .agg(open=pd.NamedAgg(column='open', aggfunc='first'),
                                high=pd.NamedAgg(column='high', aggfunc='max'),
                                low=pd.NamedAgg(column='low', aggfunc='min'),
                                close=pd.NamedAgg(column='close', aggfunc='last'),
                                volume=pd.NamedAgg(column='volume', aggfunc='sum'))
                                .reset_index()
print(df_f)
   script_id           date_time     open     high      low    close  volume
0        201 2019-01-01 10:45:00  1492.90  1495.95  1492.15  1495.85   22629
1        201 2019-01-01 10:55:00  1495.85  1495.95  1492.00  1492.05   18270
2        201 2019-01-01 11:05:00  1492.05  1493.90  1488.00  1489.25   31454
3        201 2019-01-01 11:15:00  1489.25  1492.00  1489.25  1491.50   18060
这篇关于数据帧中从第一行开始的间隔[python 3.6.0]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
				 沃梦达教程
				
			本文标题为:数据帧中从第一行开始的间隔[python 3.6.0]
 
				
         
 
            
        
             猜你喜欢
        
	     - 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 沿轴计算直方图 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
 
				 
				 
				 
				