利用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 名
相关推荐
半新半旧12 分钟前
1.DRF 环境安装与配置
python·django
Sean_summer13 分钟前
暑期第二周
前端·数据库·python
封奚泽优19 分钟前
使用Python绘制金融数据可视化工具
python·信息可视化·excel·pandas·pyplot·qtwidgets·qtcore
数据智能老司机30 分钟前
使用 Python 进行并行与高性能编程——构建多线程程序
python·性能优化·编程语言
站大爷IP33 分钟前
Python极速搭建局域网文件共享服务器:一行命令实现HTTPS安全传输
python
执器40 分钟前
SIFT实现简单的图像匹配,查找icon在图中的位置
python·图像识别
Ronin-Lotus1 小时前
程序代码篇---数据包解析
python·esp32·数据包解析
竹子_231 小时前
二分法详解:用生活例子 + 图示
python·算法·生活
2301_764441331 小时前
蜣螂优化算法的华丽转身:基于Streamlit的MSIDBO算法可视化平台
python
Leo.yuan1 小时前
数据处理工具是做什么的?常见数据处理方法介绍
大数据·数据库·人工智能·python·信息可视化