显示无索引的Pandas Dataframe

ev7lccsx  于 2023-01-07  发布在  其他
关注(0)|答案(4)|浏览(198)

我想显示没有索引列的此 Dataframe 。我正在使用制表模块来更好地可视化数据,但不想在此处看到索引。我尝试在dftabulate中使用index=False,但它不接受此参数。

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

bgibtngc

bgibtngc1#

使用tabulateshowindex = 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))
inn6fuwd

inn6fuwd2#

仅用于显示:

df.style.hide_index()

另一个与输出类似的选项(使用Markdown语法):

print(df.to_markdown(index=False))
8nuwlpux

8nuwlpux3#

你不能有没有索引的 Dataframe ,但没有索引打印漂亮,像excel工作表:

print(df.to_string(index=False))
ogq8wdun

ogq8wdun4#

对于colab中的输出,您可以使用HTML实现相同的效果:

from IPython.display import HTML

HTML(df.to_html(index=False))

相关问题