将字典值转换为excel,python中缺少第一个单元格值/名称

时间:2021-09-06 22:18:15

I have created a dictionary in python. After creating it, I wrote it into an excel file. In this file the first cell values/names alone are missing. How can I get the cell value or how can I give the cell a value by default?

我在python中创建了一个字典。创建它之后,我将其写入excel文件。在此文件中,仅缺少第一个单元格值/名称。如何获取单元格值或如何在默认情况下为单元格赋值?

-------------------------------------------------------------
**(missing value)**       a                b           c
-------------------------------------------------------------
name1                  value1           value2      value3
name2                  value1           value2      value3
------------------------------------------------------------

I have used pandas dataframe to implement this. The code i have tried is :

我使用pandas数据帧来实现这一点。我试过的代码是:

ab = pd.DataFrame(Dictionary).T 
writer = pd.ExcelWriter("ExcelFile.xlsx", engine='xlsxwriter')
ab.to_excel(writer, sheet_name='Sheet1',index='True') 

1 个解决方案

#1


1  

By default, the index is unnamed in a DataFrame. When you export it to Excel, it stores it in a separate column, but since the index does not have a name, the column is untitled.

默认情况下,索引在DataFrame中未命名。将其导出到Excel时,它会将其存储在单独的列中,但由于索引没有名称,因此该列是无标题的。

My solution would be to reset the index to its own column, and then to export it to Excel:

我的解决方案是将索引重置为自己的列,然后将其导出到Excel:

ab = pd.DataFrame(dicti) 
writer = pd.ExcelWriter("ExcelFile.xlsx", engine='xlsxwriter')
ab.reset_index(drop=False, inplace=True) #Store the index in a column named "Index"
ab.to_excel(writer, sheet_name='Sheet1', index=False) #Omit the now-irrelevant index.

#1


1  

By default, the index is unnamed in a DataFrame. When you export it to Excel, it stores it in a separate column, but since the index does not have a name, the column is untitled.

默认情况下,索引在DataFrame中未命名。将其导出到Excel时,它会将其存储在单独的列中,但由于索引没有名称,因此该列是无标题的。

My solution would be to reset the index to its own column, and then to export it to Excel:

我的解决方案是将索引重置为自己的列,然后将其导出到Excel:

ab = pd.DataFrame(dicti) 
writer = pd.ExcelWriter("ExcelFile.xlsx", engine='xlsxwriter')
ab.reset_index(drop=False, inplace=True) #Store the index in a column named "Index"
ab.to_excel(writer, sheet_name='Sheet1', index=False) #Omit the now-irrelevant index.