将Pandas数据框显示为表格

18

自从我安装了更新版本的Pandas后,每次我输入数据框的名称时,例如:

df[0:5]

要查看前几行,它会为我提供列的摘要、其中的值数量和数据类型。

那么,如何才能看到表格视图呢?(我正在使用iPython)。

提前感谢!


http://pandas.pydata.org/pandas-docs/stable/basics.html - colinfang
4个回答

23
< p >< em >注意:若要显示前几行,您也可以使用head

如果列数超过display.max_columns或长度超过display.width(行数同理),则 pandas 会显示摘要视图,因此您需要增加这些内容以显示表格视图。您可以使用 set_option 更改这些选项,例如:

pd.options.display.max_columns = 50

参见这个pandas问题:"IPython笔记本中宽DataFrame的非直观默认行为".

或者,展示前几列的前几行,可以使用:

df.head(5)[df.columns[0:4]]
# alternatively
df.iloc[:5, :4]

谢谢您的回复 - 当我输入'record_refs.head(5)[record_refs.columns[0:5]]'时,其中record_refs是数据框时,会给我一个摘要:' <class 'pandas.core.frame.DataFrame'> Int64Index: 5 entries, 0 to 4 Data columns (total 5 columns): not the table. I used to be able to type in the name of the df and get a nice table in iPython for the first few rows! - user7289
@user7289 是的,这只是针对 df[0:5] 的糖衣,问题在于 max_rows 或 display_width。 - Andy Hayden
但是有什么改变吗?我以前从未在我目前处理的列数上这样做过。 - user7289
@user7289 显示宽度? - Andy Hayden
@user7289,0.11.0版本中确实发生了一些变化,请参见此Github问题。我认为已经针对0.11.1(即将发布)进行了修复,我记得与笔记本有关... - Andy Hayden

8

简短回答:如果你的版本高于0.11.0,请尝试使用以下命令设置显示宽度:

pd.set_option('display.width', 200)

长答案: 虽然这篇帖子已经发布了4个月,但是昨晚我也遇到了同样的问题。在我的工作中正常运行的ipython笔记本,在家里无法加载HTML表格。
首先,请检查您的版本:
import pandas as pd
pd.__version__ 

我的输出结果为:'0.11.0'

接下来,请使用以下命令检查您的设置:

pd.describe_option('display')

这将给你一个所有可能的显示选项和当前默认值的长列表。 您可能需要尝试更改默认值。使用较大的值使其工作,然后根据需要缩小规模。
Display.width是最重要的一个。我将它们与导入一起放在顶部。
pd.set_option('display.height', 500)
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 200)

2

第一步: 我正在使用这个方法在eclipse的pydev中获取GUI。确保你的pydev已经设置了GUI。可以使用这个教程:http://popdevelop.com/2010/04/setting-up-ide-and-creating-a-cross-platform-qt-python-gui-application/来完成。

第二步: 从这里Fastest way to populate QTableView from Pandas data frame获取示例并进行一些修改,运行下面的代码一次:

import pandas as pd
from PyQt5 import QtCore, QtGui ,QtWidgets
class PandasModel2(QtCore.QAbstractTableModel):
    """
    Class to populate a table view with a pandas dataframe
    """
    def __init__(self, data, parent=None):
        QtCore.QAbstractTableModel.__init__(self, parent)
        self._data = data

    def rowCount(self, parent=None):
        return self._data.shape[0]

    def columnCount(self, parent=None):
        return self._data.shape[1]

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if index.isValid():
            if role == QtCore.Qt.DisplayRole:
                return str(self._data.iloc[index.row(), index.column()])
        return None

    def headerData(self, col, orientation, role):
        if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
            return self._data.columns[col]
        return None

view = QtWidgets.QTableView()

一旦完成此操作,您只需输入以下命令即可获得GUI界面。
view.setModel(PandasModel2("your dataframe name"))
view.show()

0
非常有帮助,谢谢。我正在阅读ulmo + pandas example,它是在Wakari上托管的笔记本中,对于为什么一些表格只给出了摘要而没有呈现为HTML感到困惑。看来,在Wakari中,display.width选项默认为80,因此在决定打印摘要之前,表格不必太宽。这是一个令人困惑的隐藏模式。以下内容可以解决问题:

pandas.set_option('display.width',500)

display.max_rows和display.max_columns默认为60和20,所以我将它们保留不变。这是使用Pandas 0.11.0版本时的情况。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接