简体   繁体   中英

Showing Pandas data frame as a table

since I have installed the updated version of pandas every time I type in the name of a dataframe, eg

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

Thanks in advance!

Note: To show the top few rows you can also use head .

However, pandas will show the summary view if there are more columns than display.max_columns or 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:

pd.options.display.max_columns = 50

See this pandas issue: "Unintuitive default behavior with wide DataFrames in the IPython notebook" .

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]

Short answer: If your version is later that 0.11.0, try setting your display width with:

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.

First, check your version:

import pandas as pd
pd.__version__ 

Mine comes back with: '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.

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)

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.

Second Step : taking examples from here Fastest way to populate QTableView from Pandas data frame and doing some modification, run the below code once :

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

view.setModel(PandasModel2("your dataframe name"))
view.show()

Very helpful, thanks. I was going through this ulmo + pandas example in a Notebook hosted at Wakari and 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM