0
votes

Afficher le dataframe pandas sans index

Je souhaite afficher ce dataframe sans la colonne d'index. J'utilise le module tabulate pour mieux visualiser les données, mais je ne veux pas voir l'index ici. J'ai essayé index = False dans dftabulate, mais il n'accepte pas cet 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))

entrez la description de l'image ici


1 commentaires

Vous ne pouvez pas avoir de dataframe sans index. vous pouvez définir par exemple. État de la colonne comme index.


3 Réponses :


0
votes

Juste pour afficher:

print(df.to_markdown(index=False))

Une autre option similaire à votre sortie (utilisant la syntaxe Markdown):

df.style.hide_index()


0 commentaires

0
votes

Vous ne pouvez pas avoir de dataframes sans index, mais imprimez magnifiquement sans index, comme des feuilles Excel:

print(df.to_string(index=False))


0 commentaires

2
votes

utilisez 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))


3 commentaires

Ha, juste au point de la question, en utilisant tabulate!


Parfait. c'est l'option d'index que je recherchais. Merci.


heureux de savoir. veuillez accepter la réponse si elle répond à votre objectif. Merci.