英文:
remove row index after using from_dict function with orient="index" option
问题
我已经使用以下代码在pandas中创建了一个数据框架:
import pandas as pd
a = {"a": 1, "b": 2, "c": 3}
df1 = pd.DataFrame.from_dict(a, orient="index")
print(df1)
对应的输出如下:
我想要删除第一行中显示的0。但是我无法做到。我尝试了以下操作:
df1.drop(df1.index[0], inplace=True)
这会产生不期望的输出:
我还尝试了以下操作,它会删除索引a、b和c:
df1.reset_index(inplace=True, drop=True)
我想进一步使用以下代码将这个数据框架存储到Excel中:
df1.to_excel(writer, sheet_name="Summary")
在这里,我不希望在行中有额外的0。我想知道是否有任何方法可以在数据框架中或在将其写入Excel时删除带有0的行。
英文:
I have created a dataframe using the below code in pandas:
import pandas as pd
a = {"a":1,"b":2,"c":3}
df1 = pd.DataFrame.from_dict(a,orient="index")
print(df1)
The output for the same is:
I want to remove the 0 which is seen in the first row. But I am unable to do so. I have tried the following:
df1.drop(df1.index[0],inplace=True)
This gives the following output which is not desired:
I have also tried which removes the index a,b and c.
df1.reset_index(inplace=True,drop=True)
I want to further store this data frame to Excel using the below code:
df1.to_excel(writer, sheet_name="Summary")
Here I don't want the extra 0 in the row. I would like to know if there is any way the row with 0 can be removed either in the dataframe or while writing it in Excel.
答案1
得分: 0
这不是行。 0
是列的名称。要防止该列导出到Excel,您可以传递可选参数 header=False
。
df1.to_excel('file.xlsx', sheet_name="Summary", header=False)
英文:
That's not the row. 0
is the name of the column. To prevent the column from exporting to excel you can pass the optional parameter header=False
df1.to_excel('file.xlsx', sheet_name="Summary", header=False)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论