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')

最终的结果为:

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

相关推荐
蒋胜山5 小时前
Excel 练习题(7)
经验分享·excel
蒋胜山7 小时前
Excel 练习题(6)
经验分享·excel
wcy_10119 小时前
QCoder智能生成Excel数据清洗与可视化代码
python·excel
观无10 小时前
Python读取excel并形成api接口案例
python·pandas·fastapi
JoshRen1 天前
2026教程:上传Excel,用Gemini 3镜像站多模态一键生成问卷分析图表代码与结论(附国内免费方案)
excel
chushiyunen1 天前
pandas使用笔记、数据清洗、json_normalize
笔记·pandas
实战编程2 天前
Temu 插件导出 Excel 图片问题总结(SheetJS / ExcelJS)
excel
Data-Miner2 天前
用DeepSeek V4做表:数以轻舟Agent让做Excel表像聊天一样简单
microsoft·excel
Eiceblue3 天前
使用 C# 将 Excel 转换为 Markdown 表格(含批量转换示例)
开发语言·c#·excel
Java面试题总结3 天前
使用 Python 设置 Excel 数据验证
开发语言·python·excel