Python Forum
Pandas dataframe without index - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Pandas dataframe without index (/thread-32275.html)



Pandas dataframe without index - tgottsc1 - Feb-01-2021

Hi guys,

I have a basic question about dataframes. However, my Google search has not yielded the desired results so far. I have data stored in a pandas.core.frame.DataFrame called data_stocks. When I type data_stocks and press CTRL + Enter, a simple DataFrame table is created. This table shows an index column that I would like to get rid of without changing the format of the table (that is, without converting the table to a different format).

Google search results recommended printing the table and using (index=false), however this changes the layout of the table, which is not the intention.

I would be grateful for any help!

Best regards!

Tim


RE: Pandas dataframe without index - jefsummers - Feb-01-2021

Are you just trying to hide the index from display, or do you have a reason to eliminate the index entirely (and why?) If just trying to hide it, make a dataframe from a slice of the original and display that.


RE: Pandas dataframe without index - jefsummers - Feb-01-2021

I should have tried before posting. Playing with this, I think your best bet is
df.reset_index(drop=True, inplace=True)
Though you still see the row numbers when you display
hth


RE: Pandas dataframe without index - snippsat - Feb-01-2021

From script.
print(df.to_string(index=False))
In NoteBook.
df.head().style.hide_index()
[Image: KorLwI.png]
More normal to remove index when want save a DataFrame to different format.
df.to_csv(index=False) 
df.to_html(index=False)