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

最终的结果为:

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

相关推荐
Channing Lewis9 小时前
Python读取excel转成html,并且复制excel中单元格的颜色(字体或填充)
python·html·excel
liu****12 小时前
02_Pandas_数据结构
数据结构·python·pandas·python基础
醉卧考场君莫笑18 小时前
excel数据统计与数据可视化
信息可视化·excel
weixin_4404016918 小时前
WPS Excel 宏使用
excel··wps
GalenZhang88819 小时前
Excel/WPS 表格数据合并操作指南
excel·wps
海拥✘19 小时前
Excel制作跳动爱心动画:一步步创建动态数学心形图
excel
渡我白衣20 小时前
计算机组成原理(11):加法器
python·机器学习·numpy·pandas·matplotlib·计组·数电
教练、我想打篮球20 小时前
127 apache poi3.11 写 word 中内嵌 表格换行的输出
word·excel·docx·换行
醉卧考场君莫笑1 天前
EXCEL数据分析基础(没有数据统计和数据可视化)
信息可视化·数据分析·excel
falldeep1 天前
Pandas入门指南
数据结构·算法·leetcode·pandas