python中从大到小排序

编程入门 行业动态 更新时间:2024-10-26 08:33:51

python中从<a href=https://www.elefans.com/category/jswz/34/1748170.html style=大到小排序"/>

python中从大到小排序

你好,我目前正在尝试通过和排序的数据,我在一个.csv的平均降雪量为每个月,然后选择前三个。这是我目前掌握的密码。我以为可以通过使用一个变量对数据进行排序,然后在下一行中调用已排序的变量,但这行不通。相反,我得到了一个TypeError:无法隐式地将'list'对象转换为str。任何指导都是非常感谢的。

谢谢。在import os

import csv

data = open('filteredData.csv','r')

# Create Dictionaries to store location values

# Snow_Fall is the number of inches for that location

# Number_Days is the number of days that there is Snowfall data for

Snow_Fall = {}

Number_Days = {}

# Create CSV reader

csv1 = csv.DictReader(data,delimiter=',')

# read each row of the CSV file and process it

for row in csv1:

# Check the date column and see if it is in 2017

if "2017" in row["DATE"]:

# Split the date into Month, Day and Year

Month, Day, Year = row["DATE"].split("/")

# Check to see if the value in the snow column is null/none if so then skip processing that row

if (row["SNOW"] is None) or (row["SNOW"] == ""):

pass

else:

# Check to see if the location has been added to the dict if it has then add the data to itself

# If it has not then just assign the data to the location.

# Concat the Location and Month together to get a unique location per month

# print (" The location is ==> " + row["NAME"] + " <> The Month is " + str(Month))

name_date = row["NAME"] + "_" + str(Month)

if name_date in Snow_Fall:

Snow_Fall[name_date] = Snow_Fall[name_date] + float(row["SNOW"])

Number_Days[name_date] = Number_Days[name_date] + 1

else:

Snow_Fall[name_date] = float(row["SNOW"])

Number_Days[name_date] = 1

# For each location we want to print the data for that location

for location in Snow_Fall:

# split the location into Name and month

location_name, location_date = location.split("_")

#print ("The number of inches for location " + location_name + " in the Month of " + location_date + " is " + str(Snow_Fall[location]))

#print ("The number of days of snowfall for location " + " in the Month of " + location_date+ location_name + " is " + str(Number_Days[location]))

#print ("The average Number of Inches for location " + location_name + " in the Month of " + location_date + " is " + str(Snow_Fall[location] / Number_Days[location]))

# Check if file exists then remove it if it does

if (os.path.isfile('average2017.csv')):

os.remove('average2017.csv')

# Open file

data2 = open('average2017.csv','w')

# Write File headers

data2.write("Location, Month, Average Snow Fall \n")

# Write contents of the file

for location in Snow_Fall:

# SPlit the location into name and month

location_name, location_date = location.split("_")

average = str(Snow_Fall[location] / Number_Days[location])

newaverage = sorted([average])

data2.write('"' + location_name + '",' + location_date + "," + newaverage + "\n")

# Close the open files

data2.close()

data.close()

以下信息来自测试

runfile('U:/CISP253/Final/温度py',wdir='U:/CISP253/Final')

地点月份\

0斯巴达2.8 W,密歇根州8

1斯巴达2.8 W,密歇根州9

密歇根州罗克福德2号12号

美国密歇根州罗克福德3号11号

4肯特伍德1.6 WSW,密歇根州2

5斯巴达2.8 W,密歇根州5

6斯巴达2.8 W,密歇根州6

7斯巴达2.8 W,密歇根州7

8肯特伍德1.6 WSW,密歇根州5

9 COMSTOCK PARK 3.7 NNE,密歇根州12

10喀里多尼亚4.4西北偏西,密歇根州9

11 COMSTOCK PARK 3.7 NNE,密歇根州10

12美国密歇根州东南偏东4.6号12号大急流城

13罗克福德0.9东北偏东,密歇根州5

14大急流城4.6 ESE,密歇根州10

美国密歇根州东南偏东4.6号15号急流城

16喀里多尼亚4.4西北偏西,密歇根州3

17加里多尼亚4.4西北偏西,密歇根州2

18加里多尼亚4.4西北偏西,密歇根州1

19喀里多尼亚4.4 WNW,密歇根州7

20喀里多尼亚4.4 WNW,密歇根州6

21喀里多尼亚4.4 WNW,密歇根州5

22喀里多尼亚4.4 WNW,密歇根州4

23 ROCKFORD 5.0 ESE,密歇根州8

美国密歇根州罗克福德24号7号

密歇根州洛克福德25号4号

密歇根州罗克福德26号5号

密歇根州洛克福德27号2号

密歇根州罗克福德28号3号

29大急流城2.9西北,密歇根州4

.. ... ...

美国密歇根州阿伦代尔1.3 SSW 215号

216贝尔蒙特1.0 WNW,密歇根州2

217贝尔蒙特1.0 WNW,密歇根州5

218贝尔蒙特1.0 WNW,密歇根州4

219贝尔蒙特1.0 WNW,密歇根州7

220贝尔蒙特1.0 WNW,密歇根州6

221贝尔蒙特1.0 WNW,密歇根州9

222贝尔蒙特1.0 WNW,密歇根州8

223大急流2.5 ENE,密歇根州7

224 JENISON 1.0东北,密歇根州10

密歇根州杰尼森1.0东北225号,邮编:11

226大急流杰拉尔德福特国际机场。。。12

227大急流城2.9西北,密歇根州9

228大急流杰拉尔德福特国际机场。。。10

229大急流杰拉尔德福特国际机场。。。11

美国密歇根州东大急流城230号12号

美国密歇根州东大急流城231号,邮编:11

232东大急流城,密歇根州10

233康斯托克公园3.7 NNE,密歇根州8

234肯特伍德1.6 WSW,密歇根州7

235大急流2.5 ENE,密歇根州11

美国密歇根州杰尼森1.0东北236号

237 JENISON 1.0东北,密歇根州9

美国密歇根州杰尼森1.0东北238号

239 JENISON 1.0东北,密歇根州7

密歇根州杰尼森1.0东北240号

241 JENISON 1.0东北,密歇根州5

美国密歇根州东北偏东3.7号科姆斯托克公园242号

243 COMSTOCK PARK 1.0 NNW,密歇根州7

244 ADA 1.8 W,密歇根州1

^{pr2}$

0.000000

10.000000

2 0.948387

3.40万

400万

500万

600万

700万

800万

9 1.15万

10.000000

11.000000

12239万

130万

140万

15 0.025000

16 0.408696

17 0.816667

18 0.647826

1900万

2000万

21亿

22 0.415000

2300.000000

2400.000000

25 0.90万

260万

27 1.02万

28 0.928571

29 0.362500

.. ...

2150.050000

216 0.172727

2170.025000

2180.000000

2190.000000

22000.000000

2210.000000

2220.000000

2230.000000

2240.000000

225万

226 1.193548

227亿

2280.000000

229 0.026667

230 1.264516

231 0.033333

232.000000

2330.000000

234 0.000000

2351万

2360.000000

2370万

2380.000000

239.000000

240万

241.000000

242.000000

2430.000000

244 0.609677

[245行x 3列]

回溯(最近一次呼叫):

文件“”,第1行,输入

runfile('U:/CISP253/Final/温度py',wdir='U:/CISP253/Final')

文件“C:\Users\dwillaford\AppData\Local\Continuum\anaconda3\lib\site packages\spyder\utils\site\网站自定义.py,第705行,在runfile中

execfile(文件名,命名空间)

文件“C:\Users\dwillaford\AppData\Local\Continuum\anaconda3\lib\site packages\spyder\utils\site\网站自定义.py“,第102行,在execfile中

exec(compile(f.read(),文件名,'exec'),命名空间)

文件“U:/CISP253/Final/温度py“,第5行,英寸

数据=data.sort_值(按=['平均降雪量'])

文件“C:\Users\dwillaford\AppData\Local\Continuum\anaconda3\lib\site packages\pandas\core\帧.py“,第3619行,按排序值

k=自我.xs(按,轴=其他轴)。值

文件“C:\Users\dwillaford\AppData\Local\Continuum\anaconda3\lib\site packages\pandas\core\通用.py“,第2335行,以xs表示

返回self[键]

文件“C:\Users\dwillaford\AppData\Local\Continuum\anaconda3\lib\site packages\pandas\core\py.py帧“,第2139行,ingetitem

返回self.\u getitem_列(键)

文件“C:\Users\dwillaford\AppData\Local\Continuum\anaconda3\lib\site packages\pandas\core\帧.py“,第2146行,在\u getitem_列中

返回self.\u get_item_cache(key)

文件“C:\Users\dwillaford\AppData\Local\Continuum\anaconda3\lib\site packages\pandas\core\通用.py",第1842行,在\u get_item_cache中

价值观=自我_数据.get(项)

文件“C:\Users\dwillaford\AppData\Local\Continuum\anaconda3\lib\site packages\pandas\core\内件.py“,3843排队

位置=self.items.get_loc(项)

文件“C:\Users\dwillaford\AppData\Local\Continuum\anaconda3\lib\site packages\pandas\core\index\基准.py“,第2527行,在get_loc

回归自我_引擎。获取位置(self.\u maybe\u cast_indexer(密钥))

文件“熊猫/索引.pyx“,第117行,熊猫_libs.index.IndexEngine.快去找找

文件“熊猫/索引.pyx“,第139行,熊猫_libs.index.IndexEngine.快去找找

文件“pandas/\u libs/hashtable\u class_助手.pxi,第1265行,熊猫_libs.hashtable.PyObjectHashTable.获取物品

文件“pandas/\u libs/hashtable\u class_助手.pxi“,1273行,熊猫_libs.hashtable.PyObjectHashTable.获取物品

KeyError:'平均降雪量'

更多推荐

python中从大到小排序

本文发布于:2024-02-10 20:58:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1677246.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大到   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!