发布时间:2023-08-19 13:30
给定一各地 2016 年 1 月和 2 月各个时间点的温度表格,表格预览见页面下方。
数据表的第二列表示当前时间,数据表第一行第三列到第一行最后一列表示地点。
如:表格的第二行第三列 30.36 表示:Vancouver (温哥华)在 2016-01-01 00:00:00 这一时刻的温度是 30.36 度(华氏度)。
程序给定一个日期(date)和一个地点(place),要求返回该地在这个日期下的温差(摄氏度)。
注意:华氏度需要转换成摄氏度!
华氏度转摄氏度公式:摄氏度 = (华氏度 - 32) * 5 / 9
温差:某日最高温度 - 某日最低温度
程序给定日期 date 和地点 place 的数据类型均是 str 类型;
程序返回结果的数据类型是 float 类型,如果最终结果是 numpy.float64 数据类型,请手动转换成 Python 原生的 float 数据类型;
结果需要四舍五入保留小数点后 2 位,请在得到最终结果后处理小数问题,否则可能导致结果有偏差。
输入:date = ‘2月5日’,place = ‘Dallas’
输出:-5.05
解释:查询表格可知,Dallas 在 2月 5 日温度的最高值和最低值分别是 57.09 华氏度和 34.18 华氏度,
两者的温差是 22.91 华氏度,转换为摄氏度:(22.91 - 32) * 5 / 9 = -5.05
输入:date= ‘2月18日’,place = ‘Houston’
输出:-1.78
解释:查询表格可知,Dallas 在 2月 5 日温度的最高值和最低值分别是 75.7 华氏度和 46.9 华氏度,
两者的温差是 28.8 华氏度,转换为摄氏度:(28.8 - 32) * 5 / 9 = -1.7777…. ≈ 1.78
输入:date = ‘1月21日’,place = \'Las Vegas’
输出:-1.99
import pandas class Solution: def temperature(self, date: str, place: str) -> float: # 将查询日期转换为日期字符串 month, day_ = date.split(\"月\") day, _ = day_.split(\"日\") if len(month) == 1: month = \"0\" + month if len(day) == 1: day = \"0\" + day date_str = \"2016\" + \"-\" + month + \"-\" + day # 读取数据 url = \"http://ws1.itmc.org.cn:80/JS00101/data/user/4799/208/fj_4097_temperature_2016_1_2.csv\" temperature_data = pandas.read_csv(url, sep=\",\") # 获取数据的日期字符串 temperature_data[\"day\"] = temperature_data[\"datetime\"].str[:10] # 根据日期字符串选择指定日期数据行 day = temperature_data[temperature_data[\"day\"] == date_str] # 根据地点选择选择温度数据行 city = day[place] # 温度单位转换 temperature = (city.max() - city.min() - 32) * 5 / 9 # 温度精度处理 return float(temperature.round(2)) print(Solution.temperature(Solution, date=\"2月5日\", place=\"Dallas\")) print(Solution.temperature(Solution, date=\"2月18日\", place=\"Houston\")) print(Solution.temperature(Solution, date=\"1月21日\", place=\"Las Vegas\"))
输出为:
-5.05
-1.78
-1.99
到此这篇关于pandas温差查询案例的实现的文章就介绍到这了,更多相关pandas温差查询 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
E: 无法定位软件包 ros-kinetic-usb-cam
2022-07-15/16 第一小组 田龙月 管理系统javaSE
机器学习(3)--正则化及python源码(附练习数据资源文件百度云)
Redis缓存系列:你知道redis企业实战存在的问题吗?,牛客java面试宝典pdf
php vue rbac,Vue+ThinkPHP5.1 实现基于角色控制权限的前后端分离后台管理系统
hualinux 进阶 1.9:pod[k8s最小编排单位]及pod本质(整理)
【HarmonyOS】1、HarmonyOS应用开发基础详解(持续更新)
Pycocotools 报error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Bui