使用`from_dict`函数并选择`orient=”index”`选项后,删除行索引。

huangapple go评论64阅读模式
英文:

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)

对应的输出如下:

使用`from_dict`函数并选择`orient=”index”`选项后,删除行索引。

我想要删除第一行中显示的0。但是我无法做到。我尝试了以下操作:

df1.drop(df1.index[0], inplace=True)

这会产生不期望的输出:

使用`from_dict`函数并选择`orient=”index”`选项后,删除行索引。

我还尝试了以下操作,它会删除索引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:

使用`from_dict`函数并选择`orient=”index”`选项后,删除行索引。

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:

使用`from_dict`函数并选择`orient=”index”`选项后,删除行索引。

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)

huangapple
  • 本文由 发表于 2023年7月6日 12:12:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定