Pandas在分配索引时向DataFrame添加额外的行 [英] Pandas adding extra row to DataFrame when assigning index

查看:149
本文介绍了Pandas在分配索引时向DataFrame添加额外的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用第0列(Gene.name)作为索引值。以下是原始数据:

I'm trying to use the 0th column ("Gene.name") as the Index values. Here's the original data below:

我尝试以几种不同的方式设置索引。第一个是在 DataFrame 创建中使用 index_col = 0 。我也试过 DF_mutations.index = DF_mutations [Gene.name] 但是他们都在下面的标题下面导致了一个空行:

I tried to set the index a few different ways. The first was using index_col=0 in the DataFrame creation. I also tried DF_mutations.index = DF_mutations["Gene.name"] but they both led to an empty row underneath the header show below:

当我重新分配索引值时,如何摆脱这一额外的行?

推荐答案

您看到的打印中的空行是因为索引有一个名字 - Gene.name (虽然这不是DataFrame中的实际行)。如果你不想要那一行,我相信你需要取消这个名字。示例 -

The empty row in the print you see is because the index has a name - Gene.name (This is not a real row in the DataFrame though) . If you don't want that row , I believe you would need to do away with the name. Example -

df.index.name = None

演示 -

In [6]: df = pd.DataFrame([[1,2,3,4],[1,2,3,4]]).set_index(0)

In [7]: df
Out[7]:
   1  2  3
0 <-------------------------- Name of the index for this DataFrame
1  2  3  4
1  2  3  4

In [10]: df.index.name=None

In [11]: df
Out[11]:
   1  2  3
1  2  3  4
1  2  3  4

这篇关于Pandas在分配索引时向DataFrame添加额外的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆