Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

python - Pandas adding extra row to DataFrame when assigning index

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

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: enter image description here

How can I get rid of this extra row when I'm reassigning the index values?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Demo -

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...