利用python实现对Excel文件中数据元组的自定义排序

问题引入:

假设你是一个浙江省水果超市的老板,统筹11个下辖地市的水果产量。假设11个地市生产的水果包括:苹果、香蕉和西瓜。你如何快速得到某种水果产量突出(排名前几)的地市?产量落后(排名后几)的地市?

问题分析:

得到某种水果产量排名前几和后几名的地市,本质是对Excel中的数据进行多次筛选,筛选的维度有:

1.水果种类;2.好排名;3.坏排名

现在假设一种情况:水果店老板想知道苹果产量排名前3的地市、香蕉产量排名前5的地市以及西瓜产量排名后4名的地市。

Excel本身可以通过多次筛选实现此功能,以苹果产量排名前3的地市为例 :

可以通过筛选选项选择苹果产量最大的3项,以降序呈现

得到结果:绍兴、嘉兴和宁波是苹果产量排名前3的地市

若避免和繁琐的Excel筛选菜单打交道,可以将此功能利用Python实现。

完整代码

python 复制代码
import openpyxl
 

file_path = "data.xlsx"
sheet_name = "Sheet2"
 
# 加载工作簿和工作表
workbook = openpyxl.load_workbook(file_path)
sheet = workbook[sheet_name]
 

fruit_id = 1
top = 3
bottom = 3
#data存储[地市-水果产量]的组合
data = []
 
# 读取数据
for row in sheet.iter_rows(min_row=2, values_only=True):  # 假设第一行是标题行,从第二行开始读取
    city = row[0]
    development = row[fruit_id] 
    data.append((city, development))
 
# 将数据按水果产量降序排序
sorted_data = sorted(data, key=lambda x: x[1], reverse=True)
 
# 获取水果产量前三名和后三名的地市
top_cities = [city for city, _ in sorted_data[:top]]
bottom_cities = [city for city, _ in sorted_data[-bottom:]]



print("【本日浙江省分地市水果产量情况】", end = '')
print(sheet.cell(row = 1,  column = fruit_id + 1).value)

print("👍️", end='')
for city in top_cities:
    print(city + " ", end='')
print("产量较高,排名前",top,"名")
print("❗", end='')
for city in bottom_cities:
    print(city + " ", end='')
print("产量较低,排名后",bottom,"名")

需求抽象

之前提到,筛选的维度包括1.水果种类;2.好排名;3.坏排名。

python 复制代码
fruit_id = 3 #西瓜
top = 3 # 前3名
bottom = 4 #后4名

fruit_id代表水果种类,1、2、3分别代表苹果、香蕉和西瓜;top代表前x的排名,若关心前3名的地市,则top = 3;bottom代表后x的排名,若关心后4名的地市,bottom = 4.

抽象出了产品维度之后,对各地市的水果产量进行排序:

python 复制代码
data = []
 # 读取数据
for row in sheet.iter_rows(min_row=2, values_only=True):  # 第一行是标题行,从第二行开始读取
    city = row[0]
    development = row[fruit_id] 
    data.append((city, development))

data数组存储着(地市-水果产量)的组合。row为for循环的迭代变量,可以理解为每个row为一个数组,row[0]为数组的第一个元素,对应于Excel中A列中的元素(0可以理解为数组里的下标,列的标号从0开始),并将row[0]的值赋给city;

同理,将fruit_id对应的水果产量row[fruit_id]的值赋给development;

data.append((city, development))将city和development封装在(city, development)元组中构成(地市-水果产量)组合,并随着for循环将11组(地市-水果产量)存储在data数组中。

python 复制代码
# 将数据按水果产量降序排序
sorted_data = sorted(data, key=lambda x: x[1], reverse=True)
 # 获取水果产量前三名和后三名的地市
top_cities = [city for city, _ in sorted_data[:top]]
bottom_cities = [city for city, _ in sorted_data[-bottom:]]

sorted_data利用sort函数,对data里的(地市-水果产量)组合进行排序,排序的主键是(地市-水果产量)中的水果产量(x[1]中的1为下标,表示元组中的第二个元素),reverse = True为降序排序。

排序后,可以在sorted_data数组中得到某水果产量前几和后几的地市的信息。由于sorted_data为降序(由大到小),则top代表前几,top_cities = [city for city, _ in sorted_data[:top]]存储产量为前top的地市;bottom_cities = [city for city, _ in sorted_data[-bottom:]]存储产量为后bottom的地市。

python 复制代码
print("【本日浙江省分地市水果产量情况】", end = '')
print(sheet.cell(row = 1,  column = fruit_id + 1).value)

print("👍️", end='')
for city in top_cities:
    print(city + " ", end='')
print("产量较高,排名前",top,"名")
print("❗", end='')
for city in bottom_cities:
    print(city + " ", end='')
print("产量较低,排名后",bottom,"名")

最后进行输出,并加以点评

输出结果

python 复制代码
fruit_id = 1
top = 3
bottom = 4 #求苹果产量的前3名和后4名

控制台输出:

python 复制代码
【本日浙江省分地市水果产量情况】苹果
👍️绍兴 嘉兴 宁波 产量较高,排名前 3 名
❗舟山 湖州 衢州 金华 产量较低,排名后 4 名
python 复制代码
fruit_id = 3
top = 5
bottom = 2 #求西瓜产量的前5名和后2名

控制台输出:

python 复制代码
【本日浙江省分地市水果产量情况】西瓜
👍️湖州 衢州 台州 丽水 宁波 产量较高,排名前 5 名
❗嘉兴 杭州 产量较低,排名后 2 名
相关推荐
Dxy12393102162 分钟前
DrissionPage 性能优化实战指南:让网页自动化效率飞升
运维·爬虫·python·性能优化·自动化
蹦蹦跳跳真可爱58915 分钟前
Python----目标检测(《SSD: Single Shot MultiBox Detector》论文和SSD的原理与网络结构)
人工智能·python·深度学习·神经网络·目标检测·计算机视觉
LeonDL1681 小时前
HALCON 深度学习训练 3D 图像的几种方式优缺点
人工智能·python·深度学习·3d·halcon·halcon训练3d图像·深度学习训练3d图像
在成都搬砖的鸭鸭1 小时前
【Golang】使用gin框架导出excel和csv文件
golang·excel·gin
慧都小妮子2 小时前
跨平台浏览器集成库JxBrowser 支持 Chrome 扩展程序,高效赋能 Java 桌面应用
开发语言·python·api·jxbrowser·chrome 扩展程序
tanyyinyu3 小时前
Python函数参数详解:从位置参数到灵活调用的艺术
运维·开发语言·python
qq_214782613 小时前
mac下通过anaconda安装Python
python·macos·jupyter
junyuz4 小时前
Dify docker内网部署常见问题记录
python·docker
@HNUSTer4 小时前
Python数据可视化科技图表绘制系列教程(一)
python·数据可视化·科技论文·专业制图·科研图表