Pandas 对带有 Multi-column(多列名称) 的数据排序并写入 Excel 中

Pandas 从Excel 中读取带有 Multi-column的数据

正文

我们使用如下方式写入数据:

python 复制代码
import pandas as pd
import numpy as np

df = pd.DataFrame(np.array([[10, 2, 0], [6, 1, 3], [8, 10, 7], [1, 3, 7]]), columns=[['Number', 'Name', 'Name', ], ['col 1', 'col 2', 'col 3', ]])
df.to_excel('test.xlsx')

写入后的数据顺序是杂乱无章的。

如果我们想要读取上述数据,并让它们按照 Number 列进行排序该怎么办呢?可以采用如下方法。

python 复制代码
import pandas as pd
import numpy as np

df = pd.DataFrame(np.array([[10, 2, 0], [6, 1, 3], [8, 10, 7], [1, 3, 7]]), columns=[['Number', 'Name', 'Name', ], ['col 1', 'col 2', 'col 3', ]])
df = df.sort_values([('Number', 'col 1')])
df.to_excel('test.xlsx')

最终的结果为:

如果大家觉得有用,就请点个赞吧~

相关推荐
CodeCraft Studio1 天前
Excel处理控件Aspose.Cells教程:使用 Python 将 Pandas DataFrame 转换为 Excel
python·json·excel·pandas·csv·aspose·dataframe
njxiejing1 天前
Pandas数据结构(DataFrame,字典赋值)
数据结构·人工智能·pandas
星空的资源小屋1 天前
PPTist,一个完全免费的 AI 生成 PPT 在线网站
人工智能·python·电脑·excel
开开心心_Every1 天前
免费语音合成工具:66种音色随心选
人工智能·面试·java-ee·计算机外设·电脑·maven·excel
偷心伊普西隆2 天前
EXCEL VBA 清空Excel工作表(Sheet)的方法
microsoft·excel
Coding_Doggy2 天前
苍穹外卖Day12 | Apache POI、导出Excel报表、HttpServletResponse、工作台
excel
Calihen的学习日志2 天前
【Pandas】3.1-数据预处理:列的基本操作
python·pandas
l1t3 天前
张泽鹏先生手搓的纯ANSI处理UTF-8与美团龙猫调用expat库读取Excel xml对比测试
xml·人工智能·excel·utf8·expat
Source.Liu3 天前
【Python自动化】 21.2 Pandas 读取 Excel 时的 dtype 参数完全指南
python·自动化·pandas
Source.Liu3 天前
【Python自动化】 21 Pandas Excel 操作完整指南
python·excel·pandas