Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 537
O.rka Avatar asked Oct 14 '15 05:10

O.rka


People also ask

How do I add a row to a DataFrame in pandas with index?

Use concat() to Add Row at Top of DataFrame concat([new_row,df. loc[:]]). reset_index(drop=True) to add the row to the first position of the Pandas DataFrame as Index starts from zero. reset_index() will reset the Index on the DataFrame to adjust the indexes on other rows.

How do I get rid of extra index in pandas?

The most straightforward way to drop a Pandas dataframe index is to use the Pandas . reset_index() method. By default, the method will only reset the index, forcing values from 0 - len(df)-1 as the index. The method will also simply insert the dataframe index into a column in the dataframe.

How do I add a row to a specific index?

concat() function to insert a row at any given position in the dataframe.

How do I append a row to a DataFrame?

You can create a DataFrame and append a new row to this DataFrame from dict, first create a Python Dictionary and use append() function, this method is required to pass ignore_index=True in order to append dict as a row to DataFrame, not using this will get you an error.


1 Answers

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
like image 181
Anand S Kumar Avatar answered Nov 06 '22 21:11

Anand S Kumar