英文:
multiindex for Excel output?
问题
I have a .csv to which I'd like to add what are basically multi index column names, and write to Excel. I'm just trying to determine if this is possible from the data/.csv standpoint, and then further if the output Excel can contain/retain the merged cell data.
我有一个 .csv 文件,我想要在其中添加基本上是多级索引的列名称,然后将其写入 Excel。我只是想确定是否从数据/.csv 的角度来看这是可能的,然后进一步确定输出的 Excel 是否可以包含/保留合并的单元格数据。
I've been playing with both pandas.read_csv / to_csv and also csv.writer to add rows "above" the headers, but it's unclear if/how I can use multiindex to "merge" columns in the first two rows, which are not the header? Thanks
我已经尝试使用 pandas.read_csv / to_csv 以及 csv.writer 来添加位于标题"上方"的行,但不清楚如何使用多级索引来"合并"前两行中的列,这两行不是标题?谢谢。
英文:
I have a .csv to which I'd like to add what are basically multi index column names, and write to Excel. I'm just trying to determine if this is possible from the data/.csv standpoint, and then further if the output Excel can contain/retain the merged cell data.
I've been playing with both pandas.read_csv / to_csv and also csv.writer to add rows "above" the headers, but it's unclear if/how I can use multiindex to "merge" columns in the first two rows, which are not the header? Thanks
Input .csv and desired Excel output
答案1
得分: 0
老实说,我不知道 Excel 如何处理后续的多级索引,但 CSV 标准处理多级索引的方法就是重复列名。
这将是一个多级索引的 CSV 示例:
a,a,a,a,b,b,b,b,c,c,c,c,
1,2,3,4,1,2,3,4,1,2,3,4,
我不知道 Excel 是否会将索引 "a" 显示为合并单元格。
但要小心。您想创建的表格没有 "真正的" 多级索引!"title,acres,sold" 是一个三级索引,导致所有其他列都应该有三级索引。但 "title,owner" 已经导致只有两级索引的列。
由于您的示例没有一致的规则,使用 pandas 和普通 CSV 格式化将很难弄清楚这一点。
英文:
To be honest, I don't know how excel deals with the multi-index lateron, but the csv standard to deal with multi index is just repeating the column name.
This would be a csv-example for a multi-index:<br>
a,a,a,a,b,b,b,b,c,c,c,c,<br>
1,2,3,4,1,2,3,4,1,2,3,4,<br>
I don't know if excel whould show index "a" as a merged cell.
But be careful. The table that you want to create has no "real" multi-index! Whereas "title,acres,sold" is a three staged index and leads to one column all other columns are supposed to have a three staged index as well. But "title,owner" already leads to a column with only two staged index.
Since your example does not have consistent rules it will be hard to figure this out with pandas and plain csv formatting.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论