当前位置: 首页 > news >正文

任务1.2

importpandasaspd# 导入pandas库df=pd.read_excel(r'C:\Users\Administrator\Downloads\车辆行驶里程表-2 (1).xlsx')# 读取excel文件df
车辆ID启动时间停止时间启动时剩余电量停止时剩余电量启动时电池温度峰值速度平均速度
01.02020-02-20 11:31:272020-02-20 11:59:4553451185.21931.80
11.02020-02-20 07:52:512020-02-20 08:19:256253882.79731.62
21.02020-02-13 12:44:462020-02-13 13:13:3087791376.45335.50
31.02020-02-13 07:30:162020-02-13 07:56:5894871167.23429.21
41.02020-02-10 17:21:112020-02-10 17:57:2349411382.98424.86
...........................
5992638350.02020-02-20 08:02:362020-02-20 08:21:341008823102.81363.27
5992750477.02020-02-20 17:52:272020-02-20 18:24:29968120101.98463.68
5992812070.02020-02-20 10:52:482020-02-20 11:04:369990987.35961.02
5992923950.02020-02-20 14:48:172020-02-20 15:53:09946015102.35960.12
5993067818.02020-02-20 10:44:502020-02-20 12:43:02961519102.53175.63

59931 rows × 8 columns

df_car100=df.query('车辆ID==100').reset_index(drop=True)# 筛选出车辆ID为100的行,重置索引df_car100# 查看筛选结果
车辆ID启动时间停止时间启动时剩余电量停止时剩余电量启动时电池温度峰值速度平均速度
0100.02020-02-20 16:04:112020-02-20 16:41:2758371288.9380030.59
1100.02020-02-19 15:56:412020-02-19 16:31:1173591570.3130020.87
2100.02020-02-19 11:48:332020-02-19 12:32:5186671168.0780023.02
3100.02020-02-19 10:46:442020-02-19 11:28:1092761058.7660026.07
4100.02020-02-17 16:20:482020-02-17 17:14:2433111478.6090027.99
5100.02020-02-16 16:43:552020-02-16 17:04:5154371076.0000037.26
6100.02020-02-16 13:32:332020-02-16 13:58:076444665.7500028.16
7100.02020-02-10 17:49:092020-02-10 18:11:1978621773.4690035.19
8100.02020-01-09 19:13:232020-01-09 19:40:594938770.9690028.26
9100.02020-01-05 19:47:542020-01-05 20:29:546854765.8440028.57
10100.02019-12-20 16:46:342019-12-20 17:14:1020161644.0470028.26
11100.02019-12-20 15:02:592019-12-20 15:40:1732201272.6410032.17
12100.02019-12-06 15:21:082019-12-06 15:47:1046421151.8750029.96
13100.02019-12-06 14:43:132019-12-06 15:01:435346966.5780038.92
14100.02019-11-13 12:37:452019-11-13 13:00:5362561562.0156328.53
15100.02019-11-07 13:37:072019-11-07 14:11:3393871849.0781326.14
16100.02019-09-17 12:51:052019-09-17 14:12:4997532380.9218862.40
time=pd.Timestamp(2020,1,1)# 创建一个时间戳对象,表示2020年1月1日print(time)# 打印时间戳对象df_car100_before2020=df_car100.query('停止时间<@time').reset_index(drop=True)# 筛选出车辆ID为100的行,停止时间早于2020年1月1日,重置索引df_car100_before2020# 查看筛选结果
2020-01-01 00:00:00
车辆ID启动时间停止时间启动时剩余电量停止时剩余电量启动时电池温度峰值速度平均速度
0100.02019-12-20 16:46:342019-12-20 17:14:1020161644.0470028.26
1100.02019-12-20 15:02:592019-12-20 15:40:1732201272.6410032.17
2100.02019-12-06 15:21:082019-12-06 15:47:1046421151.8750029.96
3100.02019-12-06 14:43:132019-12-06 15:01:435346966.5780038.92
4100.02019-11-13 12:37:452019-11-13 13:00:5362561562.0156328.53
5100.02019-11-07 13:37:072019-11-07 14:11:3393871849.0781326.14
6100.02019-09-17 12:51:052019-09-17 14:12:4997532380.9218862.40
df_car100_before2020=df_car100.loc[df_car100['停止时间']>time,:].reset_index(drop=True)# 筛选出车辆ID为100的行,停止时间晚于2020年1月1日,重置索引df_car100_before2020# 查看筛选结果
车辆ID启动时间停止时间启动时剩余电量停止时剩余电量启动时电池温度峰值速度平均速度
0100.02020-02-20 16:04:112020-02-20 16:41:2758371288.93830.59
1100.02020-02-19 15:56:412020-02-19 16:31:1173591570.31320.87
2100.02020-02-19 11:48:332020-02-19 12:32:5186671168.07823.02
3100.02020-02-19 10:46:442020-02-19 11:28:1092761058.76626.07
4100.02020-02-17 16:20:482020-02-17 17:14:2433111478.60927.99
5100.02020-02-16 16:43:552020-02-16 17:04:5154371076.00037.26
6100.02020-02-16 13:32:332020-02-16 13:58:076444665.75028.16
7100.02020-02-10 17:49:092020-02-10 18:11:1978621773.46935.19
8100.02020-01-09 19:13:232020-01-09 19:40:594938770.96928.26
9100.02020-01-05 19:47:542020-01-05 20:29:546854765.84428.57
df_car100_before2020['电量消耗']=df_car100_before2020['启动时剩余电量']-df_car100_before2020['停止时剩余电量']# 计算车辆ID为100的行,启动时剩余电量减去停止时剩余电量,得到电量消耗df_car100_before2020# 查看筛选结果
C:\Users\Administrator\AppData\Local\Temp\ipykernel_8068\2176885489.py:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df_car100_before2020['电量消耗'] = df_car100_before2020['启动时剩余电量'] - df_car100_before2020['停止时剩余电量']
车辆ID启动时间停止时间启动时剩余电量停止时剩余电量启动时电池温度峰值速度平均速度电量消耗行驶时长delta行驶时长s行驶里程
10100.02019-12-20 16:46:342019-12-20 17:14:1020161644.0470028.2640 days 00:27:36165613.0
11100.02019-12-20 15:02:592019-12-20 15:40:1732201272.6410032.17120 days 00:37:18223820.0
12100.02019-12-06 15:21:082019-12-06 15:47:1046421151.8750029.9640 days 00:26:02156213.0
13100.02019-12-06 14:43:132019-12-06 15:01:435346966.5780038.9270 days 00:18:30111012.0
14100.02019-11-13 12:37:452019-11-13 13:00:5362561562.0156328.5360 days 00:23:08138811.0
15100.02019-11-07 13:37:072019-11-07 14:11:3393871849.0781326.1460 days 00:34:26206615.0
16100.02019-09-17 12:51:052019-09-17 14:12:4997532380.9218862.40440 days 01:21:44490485.0
df_car100_before2020['行驶时长delta']=df_car100_before2020['停止时间']-df_car100_before2020['启动时间']# 计算车辆ID为100的行,停止时间减去启动时间,得到行驶时长df_car100_before2020['行驶时长s']=df_car100_before2020['行驶时长delta'].dt.seconds# 将行驶时长转换为秒df_car100_before2020['行驶里程']=(df_car100_before2020['行驶时长s']/60/60*df_car100_before2020['平均速度']).round(0)# 计算车辆ID为100的行,行驶时长除以60再除以60,得到行驶里程,乘以平均速度,得到行驶里程,保留0位小数df_car100_before2020
C:\Users\Administrator\AppData\Local\Temp\ipykernel_8068\4107842984.py:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df_car100_before2020['行驶时长delta'] = df_car100_before2020['停止时间'] - df_car100_before2020['启动时间'] C:\Users\Administrator\AppData\Local\Temp\ipykernel_8068\4107842984.py:2: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df_car100_before2020['行驶时长s'] = df_car100_before2020['行驶时长delta'].dt.seconds C:\Users\Administrator\AppData\Local\Temp\ipykernel_8068\4107842984.py:3: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df_car100_before2020['行驶里程'] = (df_car100_before2020['行驶时长s']/60/60 * df_car100_before2020['平均速度']).round(0)
车辆ID启动时间停止时间启动时剩余电量停止时剩余电量启动时电池温度峰值速度平均速度电量消耗行驶时长delta行驶时长s行驶里程
10100.02019-12-20 16:46:342019-12-20 17:14:1020161644.0470028.2640 days 00:27:36165613.0
11100.02019-12-20 15:02:592019-12-20 15:40:1732201272.6410032.17120 days 00:37:18223820.0
12100.02019-12-06 15:21:082019-12-06 15:47:1046421151.8750029.9640 days 00:26:02156213.0
13100.02019-12-06 14:43:132019-12-06 15:01:435346966.5780038.9270 days 00:18:30111012.0
14100.02019-11-13 12:37:452019-11-13 13:00:5362561562.0156328.5360 days 00:23:08138811.0
15100.02019-11-07 13:37:072019-11-07 14:11:3393871849.0781326.1460 days 00:34:26206615.0
16100.02019-09-17 12:51:052019-09-17 14:12:4997532380.9218862.40440 days 01:21:44490485.0
soc_div_odo_before2020=df_car100_before2020['电量消耗'].sum()/df_car100_before2020['行驶里程'].sum()# 计算车辆ID为100的行,电量消耗的总和除以行驶里程的总和,得到电量消耗与行驶里程的比例soc_div_odo_before2020# 查看结果
0.4911242603550296
defget_soc_div_odo(df):""" df: 需要计算【总电量消耗/总行驶里程】的表格 Return: 总电量消耗/总行驶里程 """df['电量消耗']=df['启动时剩余电量']-df['停止时剩余电量']df['行驶时长delta']=df['停止时间']-df['启动时间']df['行驶时长s']=df['行驶时长delta'].dt.seconds df['行驶里程']=(df['行驶时长s']/60/60*df['平均速度']).round(0)returndf['电量消耗'].sum()/df['行驶里程'].sum()
get_soc_div_odo(df_car100_before2020)# 调用函数get_soc_div_odo,计算车辆ID为100的行,电量消耗与行驶里程的比例
C:\Users\Administrator\AppData\Local\Temp\ipykernel_8068\1796169913.py:6: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df['电量消耗'] = df['启动时剩余电量'] - df['停止时剩余电量'] C:\Users\Administrator\AppData\Local\Temp\ipykernel_8068\1796169913.py:7: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df['行驶时长delta'] = df['停止时间'] - df['启动时间'] C:\Users\Administrator\AppData\Local\Temp\ipykernel_8068\1796169913.py:8: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df['行驶时长s'] = df['行驶时长delta'].dt.seconds C:\Users\Administrator\AppData\Local\Temp\ipykernel_8068\1796169913.py:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df['行驶里程'] = (df['行驶时长s']/60/60 * df['平均速度']).round(0) 0.4911242603550296
df_car100_after2020=df_car100[df_car100['启动时间']>='2020-01-01']# 筛选出车辆ID为100的行,启动时间晚于2020年1月1日print(f"2020年后数据量:{len(df_car100_after2020)}条")# 查看筛选结果的行数df_car100_after2020.head()# 查看筛选结果的前五行
2020年后数据量:10 条
车辆ID启动时间停止时间启动时剩余电量停止时剩余电量启动时电池温度峰值速度平均速度
0100.02020-02-20 16:04:112020-02-20 16:41:2758371288.93830.59
1100.02020-02-19 15:56:412020-02-19 16:31:1173591570.31320.87
2100.02020-02-19 11:48:332020-02-19 12:32:5186671168.07823.02
3100.02020-02-19 10:46:442020-02-19 11:28:1092761058.76626.07
4100.02020-02-17 16:20:482020-02-17 17:14:2433111478.60927.99
get_soc_div_odo(df_car100_after2020)# 调用函数get_soc_div_odo,计算车辆ID为100的行,电量消耗与行驶里程的比例
C:\Users\Administrator\AppData\Local\Temp\ipykernel_8068\1796169913.py:6: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df['电量消耗'] = df['启动时剩余电量'] - df['停止时剩余电量'] C:\Users\Administrator\AppData\Local\Temp\ipykernel_8068\1796169913.py:7: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df['行驶时长delta'] = df['停止时间'] - df['启动时间'] C:\Users\Administrator\AppData\Local\Temp\ipykernel_8068\1796169913.py:8: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df['行驶时长s'] = df['行驶时长delta'].dt.seconds C:\Users\Administrator\AppData\Local\Temp\ipykernel_8068\1796169913.py:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df['行驶里程'] = (df['行驶时长s']/60/60 * df['平均速度']).round(0) 1.0493827160493827
http://www.jsqmd.com/news/619012/

相关文章:

  • 企业级Linux内核测试解决方案:LTP架构设计与生产环境实践
  • 如何选择适合的液相色谱仪?品牌与性能全解析 - 品牌推荐大师
  • 开发者心理健康:高压环境下的生存法则——软件测试从业者的专业自救指南
  • AI时代新型的项目管理应该是什么样的?亚
  • 3步构建企业级WebDAV文件服务:高效安全的远程文件访问方案
  • 0经验也能做好Ozon运营?Captain AI让中小卖家轻松突围
  • 2026室内3D地图建模软件测评:功能、易用性与价格 - 品牌2025
  • 开源贡献者:隐形职业加速器
  • Flagr性能优化秘籍:如何达到每秒2000次评估请求
  • 不止于单机:如何用RflySim的分布式架构和UE5视景,在实验室里跑通百架无人机集群算法仿真
  • ControlNet-v1-1 FP16 Safetensors完整指南:如何精准控制AI图像生成
  • K折交叉验证实战指南——从cross_val_score到模型调优
  • AI Agent 跑完任务怎么通知你?我写了个微信推送服务页
  • 10年网安老兵的真心劝退:这四类人,请先看完这份“避坑指南”再决定!
  • 深夜告警炸裂?这份Linux故障排查“作战地图”请收好搪
  • 直播预告| HOW 2026 剧透!PostgreSQLAI 专访 + 福利门票速领
  • 大学生HTML期末大作业——HTML+CSS+JavaScript培训机构(画室)
  • KMS_VL_ALL_AIO:Windows与Office批量授权智能激活解决方案
  • 论文被识别AI写作怎么办?深度降AI方案帮你消痕迹
  • PotPlayer字幕翻译终极指南:5步实现外语视频无障碍实时翻译
  • 3步永久备份QQ空间历史数据:GetQzonehistory终极指南
  • 2026室内3D地图制作实战指南:从CAD到上线的全流程解析 - 品牌2025
  • Android Studio中文语言包深度解析:从界面本地化到开发效率提升
  • Juju最佳实践:7个技巧提升应用部署效率和运维稳定性
  • 如何用Draw.io ECE库快速绘制专业电路图:免费电子工程绘图终极指南
  • 四轴码垛机器人运动学解析:从DH建模到轨迹规划
  • SpringBoot集成redisson自带分布式锁-快速集成亲测可用
  • 如何构建高性能帧同步游戏:ET框架预测回滚技术深度解析
  • JMS, ActiveMQ 学习一则纲
  • muffet源码解析:深入理解HTTP客户端池与并发控制机制