首页 > 解决方案 > 在 Google Colaboratory 中显示 pandas 数据框时出错

问题描述

我在 Google Colab python(3.6) 笔记本中构建了一个数据分析管道。每当我尝试内联显示 pandas(0.23.4) 数据框时,都会出现以下错误:

TypeError: init () got an unexpected keyword argument 'max_rows'

似乎唯一能解决问题的是创建一个新的 colab 笔记本。(文件>新笔记本)。但即便如此,在使用一段时间后最终还是会遇到同样的问题。

此外,重要的是要注意数据框可以正常工作以进行计算和访问值。显示它似乎是一个问题。

df.head()

结果是:


TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/IPython/core/formatters.py in __call__(self, obj)
    336             method = get_real_method(obj, self.print_method)
    337             if method is not None:
--> 338                 return method()
    339             return None
    340         else:

2 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in _repr_html_(self)
    694         See Also
    695         --------
--> 696         to_html : Convert DataFrame to HTML.
    697 
    698         Examples

/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in to_html(self, buf, columns, col_space, header, index, na_rep, formatters, float_format, sparsify, index_names, justify, bold_rows, classes, escape, max_rows, max_cols, show_dimensions, notebook, decimal, border, table_id)
   2035             Dictionary mapping columns containing datetime types to stata
   2036             internal format to use when writing the dates. Options are 'tc',
-> 2037             'td', 'tm', 'tw', 'th', 'tq', 'ty'. Column can be either an integer
   2038             or a name. Datetime columns that do not have a conversion type
   2039             specified will be converted to 'tc'. Raises NotImplementedError if

/usr/local/lib/python3.6/dist-packages/pandas/io/formats/format.py in to_html(self, classes, notebook, border)
    751             need_leadsp = dict(zip(fmt_columns, map(is_numeric_dtype, dtypes)))
    752 
--> 753             def space_format(x, y):
    754                 if (y not in self.formatters and
    755                         need_leadsp[x] and not restrict_formatting):

TypeError: __init__() got an unexpected keyword argument 'max_rows'

预期结果是将数据框显示在 html 表中。

标签: python-3.xpandasjupyter-notebookgoogle-colaboratory

解决方案


您可以尝试使用IPython display(请参阅此处的第 3 点)

from IPython import display
display(df.head())

或者,将这两行添加到您的导入中并且您不需要display此处说明)

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

df.head()

推荐阅读