在所有可用记录中,哪个月的最大阵风速度中位数最高

2024-05-12 00:22:00 发布

您现在位置:Python中文网/ 问答频道 /正文

在所有可用记录中,哪个月的最大阵风速度中位数最高。还可以找到相应的值

数据集如下所示

Day Average temperature (°F)    Average humidity (%)    Average dewpoint (°F)   Average barometer (in)  Average windspeed (mph) Average gustspeed (mph) Average direction (°deg)    Rainfall for month (in) Rainfall for year (in)  Maximum rain per minute Maximum temperature (°F)    Minimum temperature (°F)    Maximum humidity (%)    Minimum humidity (%)    Maximum pressure    Minimum pressure    Maximum windspeed (mph) Maximum gust speed (mph)    Maximum heat index (°F)
0   1/01/2009   37.8    35  12.7    29.7    26.4    36.8    274 0.0 0.0 0.0 40.1    34.5    44  27  29.762  29.596  41.4    59.0    40.1
1   2/01/2009   43.2    32  14.7    29.5    12.8    18.0    240 0.0 0.0 0.0 52.8    37.5    43  16  29.669  29.268  35.7    51.0    52.8
2   3/01/2009   25.7    60  12.7    29.7    8.3 12.2    290 0.0 0.0 0.0 41.2    6.7 89  35  30.232  29.260  25.3    38.0    41.2
3   4/01/2009   9.3 67  0.1 30.4    2.9 4.5 47  0.0 0.0 0.0 19.4    -0.0    79  35  30.566  30.227  12.7    20.0    32.0
4   5/01/2009   23.5    30  -5.3    29.9    16.7    23.1    265 0.0 0.0 0.0 30.3    15.1    56  13  30.233  29.568  38.0    53.0    32.0

我写的代码如下,但是测试用例失败了 代码:

data1= data[data['Maximum gust speed (mph)']!= 0.0]
#print(data1.count())
#print(data.count())
#print(data.median())
#print(data1.median())
max_gust_value_median = data1.groupby(pd.DatetimeIndex(data1['Day']).month).agg({'Maximum gust speed (mph)':pd.Series.median})
#print(max_gust_value_median)
max_gust_month = "max_gust_month = " + str(max_gust_value_median.idxmax()[0])
max_gust_value = "max_gust_value = " + format((max_gust_value_median.max()[0]),'.2f')
print(max_gust_value)
print(max_gust_month)

输出: 最大阵风值=32.20 最大阵风月=11

错误: ==============================================================================================================================

(小标题)(小标题)(小标题)(小标题)(小标题)(小标题)(小标题)(小标题)(小标题)(小标题)小标题

def test_max_gust_month():
  assert hash_dict["max_gust_month"] == answer_dict["max_gust_month"]

E断言错误:断言“d1aecb72eff6…7412c2a651d81”==“e6e3cedb0dc6…798711404a6c8”

E-e6e3cedb0dc67a96317798711404a6c8

E+d1aecb72eff64d1169f7412c2a651d81

test.py:52:AssertionError

测试最大阵风值

def test_max_gust_value():
  assert hash_dict["max_gust_value"] == answer_dict["max_gust_value"]

E断言错误:断言“6879064548a1…2361f91ecd7b0”==“5818ebe448c4…471e93c92d545”

E-5818ebe448c43f2dfed471e93c92d545

E+6879064548A136DA2F2361F91ECD7B0

test.py:55:AssertionError

===========================================================================================

失败的测试。py::test_max_gust_month-AssertionError:assert'd1aecb72eff6

失败的测试。py::test_max_gust_value-AssertionError:assert'6879064548a1

=================================================2失败,9以0.13秒的速度通过====================================================


Tags: testdatavalueassertmaxmedianprintaverage
1条回答
网友
1楼 · 发布于 2024-05-12 00:22:00

以下代码

data['Month'] = pd.to_datetime(data['Day'], dayfirst=True).dt.strftime('%B')
month_list =['January', 'February','March','April', 'May', 'June','July','August','September','October','November','December']
month_grp = data.groupby(['Month'])
month_name_value_all = []
max_value=[]
for i in month_list:
    month_name_value =[]
    value = month_grp.get_group(i).median().loc['Maximum gust speed (mph)']
    month_name_value.append(i)
    max_value.append(value)
    month_name_value.append(value)
    month_name_value_all.append(month_name_value)

max_gust_value = format(max(max_value), '.2f')

for j in month_name_value_all:
    month_max_find =[]
    month_max_find.append(j)
    if max_gust_value in j:
        break

max_gust_month = month_max_find[0][0]

print("max_gust_value =   ", max_gust_value) 
print("max_gust_month =   ", max_gust_month)

相关问题 更多 >