使用openpyxl交换行

2024-09-28 15:02:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个对象的电子表格,这些对象的新值每天都会生成,并保存在旧单元格旁边的新单元格中。在

我想按我的日期来创造一个新的价值。在

我可以编写代码来根据值的大小进行排序,我只是有点困惑,我将如何移动与排序值相关的行?也许创建一个列表列表,其中每个子列表包含每行的数据。然后按最新值对应的元素排序。听起来有点复杂有人有更好的主意吗?以下是数据https://github.com/mrsmn/coinmarketcap-api/files/71682/new_values2.xlsx的示例

谢谢


Tags: 数据对象代码httpsgithubcom元素列表
1条回答
网友
1楼 · 发布于 2024-09-28 15:02:23

Maybe create a list of lists, where each sub list contains data for each row. Then sort by the element corresponding to the latest

不要为这些烦恼,使用一个数据帧。对其进行排序,然后重新保存excel文件。在

import pandas as pd

df = pd.read_excel(input_excel_path, sheetname=sheet_name)
df.sort(column_name_to_sort_by, inplace=True, ascending=False) # the default for 'ascending' is True
df.to_excel(output_excel_path)

相关问题 更多 >