利用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 名
相关推荐
StarPrayers.4 分钟前
PySpark基础知识(python)
python·数据分析·spark
潜龙952741 分钟前
第6.2节 Android Agent开发<二>
android·python·覆盖率数据
I'm a winner1 小时前
第五章:Python 数据结构:列表、元组与字典(一)
开发语言·数据结构·python
揭老师高效办公1 小时前
在Excel和WPS表格中批量删除数据区域的批注
excel·wps表格
我是zxb1 小时前
EasyExcel:快速读写Excel的工具类
数据库·oracle·excel
番薯大佬2 小时前
Python学习-day9 字典Dictionary
网络·python·学习
nightunderblackcat2 小时前
新手向:C语言、Java、Python 的选择与未来指南
java·c语言·python
夏日麋鹿~2 小时前
逐时nc数据批量处理为日平均
python
程序员三明治2 小时前
Python编辑器的安装及配置(Pycharm、Jupyter的安装)从0带你配置,小土堆视频
python·pycharm·编辑器
理想国的女研究僧2 小时前
Jupyter Notebook操作指南(1)
ide·python·学习·jupyter