Python 将 Pandas 数据框显示为表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16958513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Showing Pandas data frame as a table
提问by user7289
since I have installed the updated version of pandas every time I type in the name of a dataframe, e.g.
因为我每次输入数据框的名称时都安装了更新版本的熊猫,例如
df[0:5]
To see the first few rows, it gives me a summary of the columns, the number of values in them and the data types instead.
要查看前几行,它会为我提供列的摘要、其中的值数量以及数据类型。
How do I get to see the tabular view instead? (I am using iPython btw).
我如何才能看到表格视图?(顺便说一句,我正在使用 iPython)。
Thanks in advance!
提前致谢!
采纳答案by Andy Hayden
Note: To show the top few rows you can also use head.
注意:要显示前几行,您还可以使用head.
However, pandas will show the summary view if there are more columns than display.max_columnsor they are longer than display.width(analogously for rows), so you'll need to increase these to show a tabular view. You can change these using set option, for example:
但是,如果列数多于display.max_columns或长于display.width(类似于行),pandas 将显示摘要视图,因此您需要增加这些以显示表格视图。您可以使用 set 选项更改这些,例如:
pd.options.display.max_columns = 50
See this pandas issue: "Unintuitive default behavior with wide DataFrames in the IPython notebook".
请参阅此熊猫问题:“IPython 笔记本中宽数据帧的不直观默认行为”。
Alternatively, show the first few rows for the first few columns you can use:
或者,显示您可以使用的前几列的前几行:
df.head(5)[df.columns[0:4]]
# alternatively
df.iloc[:5, :4]
回答by Lance Smith
Short answer: If your version is later that 0.11.0, try setting your display width with:
简短回答:如果您的版本高于 0.11.0,请尝试使用以下方法设置显示宽度:
pd.set_option('display.width', 200)
Long answer: Granted, the thread is 4 months old, but I was having this same problem last night. An ipython notebook that worked fine at my job would not present html tables at home.
长答案:当然,该线程已有 4 个月大,但我昨晚遇到了同样的问题。在我的工作中运行良好的 ipython 笔记本不会在家里显示 html 表。
First, check your version:
首先,检查您的版本:
import pandas as pd
pd.__version__
Mine comes back with: '0.11.0'
我的回来了:'0.11.0'
Next, check your settings with:
接下来,使用以下命令检查您的设置:
pd.describe_option('display')
This will give you a long list of all possible display options and the current default values. You may have to play with changing the defaults. Go with large values to get it working and then scale back as needed.
这将为您提供所有可能的显示选项和当前默认值的长列表。您可能不得不尝试更改默认值。使用大值使其工作,然后根据需要缩小。
Display.width is the important one. I put these at the top with imports.
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)
回答by Nick Edgar
Very helpful, thanks. I was going through this ulmo + pandas examplein a Notebook hosted at Wakariand was puzzled by why some tables were just giving a summary, not rendering as HTML. It appears that the display.width option in Wakari defaults to only 80, so the table doesn't have to be very wide before it decides to print a summary instead. This is a confusing hidden mode. The following fixed it up:
很有帮助,谢谢。我正想通过这个船型果木+大熊猫例如在托管的笔记本Wakari,并明白为什么有些表格只是在向一个总结,而不是渲染为HTML不解。看起来 Wakari 中的 display.width 选项默认仅为 80,因此在决定打印摘要之前,表格不必非常宽。这是一个令人困惑的隐藏模式。以下修复了它:
pandas.set_option('display.width',500)
display.max_rows and display.max_columns default to 60 and 20, so I left those as-is. This was with Pandas 0.11.0.
display.max_rows 和 display.max_columns 默认为 60 和 20,所以我保持原样。这是 Pandas 0.11.0。
回答by niths4u
First Step :I am using this to get GUI in pydev of eclipse. Make sure your pydev has GUI set . Use this tutorial : http://popdevelop.com/2010/04/setting-up-ide-and-creating-a-cross-platform-qt-python-gui-application/for doing that.
第一步:我使用它在 Eclipse 的 pydev 中获取 GUI。确保您的 pydev 设置了 GUI。使用本教程:http: //popdevelop.com/2010/04/setting-up-ide-and-creating-a-cross-platform-qt-python-gui-application/来做这件事。
Second Step :taking examples from here Fastest way to populate QTableView from Pandas data frameand doing some modification, run the below code once :
第二步:从这里获取示例 从Pandas 数据框填充 QTableView 的最快方法并进行一些修改, 运行以下代码一次:
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()
Once this is done . All you need is simply type the below commands and you can have the GUI
一旦完成。您所需要的只是简单地输入以下命令,您就可以拥有 GUI
view.setModel(PandasModel2("your dataframe name"))
view.show()

