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

最终的结果为:

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

相关推荐
2501_9307077822 分钟前
如何使用C#代码在Excel 文件中添加工作表
excel
shouchaobao6 小时前
免费PDF工具:PDF转Word/Excel/图片+AI总结+合并拆分+OCR识别,多端无广告!
pdf·word·excel
AI小云15 小时前
【数据操作与可视化】Pandas数据处理-其他操作
python·pandas
allbs16 小时前
spring boot项目excel导出功能封装——4.导入
spring boot·后端·excel
m5655bj17 小时前
使用 Python 高效复制 Excel 行、列、单元格
开发语言·python·excel
温轻舟1 天前
Python自动办公工具01-Excel文件编辑器
开发语言·python·编辑器·excel·温轻舟
WarPigs1 天前
Unity编辑器开发笔记
unity·编辑器·excel
allbs2 天前
spring boot项目excel导出功能封装——3.图表导出
spring boot·后端·excel
lqz19932 天前
根据html导出excel和word
html·word·excel
12程序猿2 天前
postman调用文件(.xlsm---带宏的excel文件)下载接口成功下载excel文件,浏览器访问下载文件打不开
excel·lua·postman