6

I want to display this dataframe without the index column. i am using tabulate module to better visualize the data, but don't want to see the index here. i tried index=False in dftabulate, but it doesn't accept this argument.

import pandas as pd
from tabulate import tabulate

# initialize list of lists
states = [['Alabama - AL', 'Alaska - AK', 'Arizona - AZ', 'Arkansas - AR', 'California - CA'],
               ['Colorado - CO', 'Connecticut - CT', 'Delaware - DE', 'Florida - FL', 'Georgia - GA'],
               ['Hawaii - HI', 'Idaho - ID', 'Illinois - IL', 'Indiana - IN', 'Iowa - IA'],
               ['Kansas - KS', 'Kentucky - KY', 'Louisiana - LA', 'Maine - ME', 'Maryland - MD'],
               ['Massachusetts - MA', 'Michigan - MI', 'Minnesota - MN', 'Mississippi - MS', 'Missouri - MO'],
               ['Montana - MT', 'Nebraska - NE', 'Nevada - NV', 'New Hampshire - NH', 'New Jersey - NJ'],
               ['New Mexico - NM', 'New York - NY', 'North Carolina - NC', 'North Dakota - ND', 'Ohio - OH'],
               ['Oklahoma - OK', 'Oregon - OR', 'Pennsylvania - PA', 'Rhode Island - RI', 'South Carolina - SC'],
               ['South Dakota - SD', 'Tennessee - TN', 'Texas - TX', 'Utah - UT', 'Vermont - VT'],
               ['Virginia - VA', 'Washington - WA', 'West Virginia - WV', 'Wisconsin - WI', 'Wyoming - WY']]

# Create the pandas DataFrame
df = pd.DataFrame(states, columns=['State - Abbreviation', 'State - Abbreviation', 'State - Abbreviation', 'State - Abbreviation', 'State - Abbreviation'])
pdtabulate = lambda df: tabulate(df, headers='keys', tablefmt='psql')

# print dataframe.
print(pdtabulate(df))

enter image description here

hiker
  • 163
  • 1
  • 1
  • 8
  • 1
    You cannot have a dataframe without index. you can set for example. State column as index. – Mehdi Golzadeh Nov 15 '20 at 15:57
  • Does this answer your question? [Pandas dataframe hide index functionality?](https://stackoverflow.com/questions/21256013/pandas-dataframe-hide-index-functionality) – questionto42 Jan 05 '23 at 18:38

4 Answers4

7

Just for displaying (valid only for notebooks):

df.style.hide_index()

Another option similar to your output (using Markdown syntax):

print(df.to_markdown(index=False))
Yuca
  • 6,010
  • 3
  • 22
  • 42
Cainã Max Couto-Silva
  • 4,839
  • 1
  • 11
  • 35
7

use tabulate showindex = False:

import pandas as pd
from tabulate import tabulate


# Create the pandas DataFrame
df = pd.DataFrame(states, columns=['State - Abbreviation', 'State - Abbreviation', 'State - Abbreviation', 'State - Abbreviation', 'State - Abbreviation'])
pdtabulate = lambda df: tabulate(df, headers='keys', tablefmt='psql', showindex=False)

print(pdtabulate(df))
Tasnuva Leeya
  • 2,515
  • 1
  • 13
  • 21
3

You can't have dataframes without index, but print without index beautifully, like excel sheets:

print(df.to_string(index=False))
Wasif
  • 14,755
  • 3
  • 14
  • 34
1

For output in colab you can achieve the same with HTML:

from IPython.display import HTML

HTML(df.to_html(index=False))
James Hirschorn
  • 7,032
  • 5
  • 45
  • 53