如何在 iPython notebook 中预览大 pandas DataFrame 的一部分? [英] How to preview a part of a large pandas DataFrame, in iPython notebook?

查看:25
本文介绍了如何在 iPython notebook 中预览大 pandas DataFrame 的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始在 IPython Notebook 中使用 pandas,遇到以下问题:当从 CSV 文件读取的 DataFrame 很小时,IPython Notebook 将其显示在一个漂亮的表格视图中.当 DataFrame 很大时,输出如下:

在 [27] 中:评估 = readCSV("evaluation_MO_without_VNS_quality.csv").filter(["solver", "instance", "runtime", "objective"])在 [37] 中:评估出[37]:<class 'pandas.core.frame.DataFrame'>Int64Index:333 个条目,0 到 332数据列:求解器 333 个非空值实例 333 非空值运行时 333 非空值目标 333 非空值数据类型:int64(1),对象(3)

我希望将数据框的一小部分视为表格,以确保其格式正确.我有哪些选择?

解决方案

在这种情况下,DataFrame 很长但不太宽,您可以简单地将其切片:

<预><代码>>>>df = pd.DataFrame({"A": range(1000), "B": range(1000)})>>>df<class 'pandas.core.frame.DataFrame'>Int64Index:1000 个条目,0 到 999数据列:A 1000 个非空值B 1000 个非空值数据类型:int64(2)>>>df[:5]甲乙0 0 01 1 12 2 23 3 34 4 4

ix 已弃用.

如果既宽又长,我倾向于使用.ix:

<预><代码>>>>df = pd.DataFrame({i: range(1000) for i in range(100)})>>>df.ix[:5, :10]0 1 2 3 4 5 6 7 8 9 100 0 0 0 0 0 0 0 0 0 0 01 1 1 1 1 1 1 1 1 1 1 12 2 2 2 2 2 2 2 2 2 2 23 3 3 3 3 3 3 3 3 3 3 34 4 4 4 4 4 4 4 4 4 4 45 5 5 5 5 5 5 5 5 5 5 5

I am just getting started with pandas in the IPython Notebook and encountering the following problem: When a DataFrame read from a CSV file is small, the IPython Notebook displays it in a nice table view. When the DataFrame is large, something like this is ouput:

In [27]:

evaluation = readCSV("evaluation_MO_without_VNS_quality.csv").filter(["solver", "instance", "runtime", "objective"])

In [37]:

evaluation

Out[37]:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 333 entries, 0 to 332
Data columns:
solver       333  non-null values
instance     333  non-null values
runtime      333  non-null values
objective    333  non-null values
dtypes: int64(1), object(3)

I would like to see a small portion of the data frame as a table just to make sure it is in the right format. What options do I have?

解决方案

In this case, where the DataFrame is long but not too wide, you can simply slice it:

>>> df = pd.DataFrame({"A": range(1000), "B": range(1000)})
>>> df
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1000 entries, 0 to 999
Data columns:
A    1000  non-null values
B    1000  non-null values
dtypes: int64(2)
>>> df[:5]
   A  B
0  0  0
1  1  1
2  2  2
3  3  3
4  4  4

ix is deprecated.

If it's both wide and long, I tend to use .ix:

>>> df = pd.DataFrame({i: range(1000) for i in range(100)})
>>> df.ix[:5, :10]
   0   1   2   3   4   5   6   7   8   9   10
0   0   0   0   0   0   0   0   0   0   0   0
1   1   1   1   1   1   1   1   1   1   1   1
2   2   2   2   2   2   2   2   2   2   2   2
3   3   3   3   3   3   3   3   3   3   3   3
4   4   4   4   4   4   4   4   4   4   4   4
5   5   5   5   5   5   5   5   5   5   5   5

这篇关于如何在 iPython notebook 中预览大 pandas DataFrame 的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆