在控制台中打印数据时,Pandas数据框仅显示外部列

问题描述 投票:0回答:1

相对较新的Python和Pandas,但我经历了很多堆栈溢出和谷歌,并找不到任何与我遇到的问题相同的问题。 (也许我只是使用了错误的关键词,但我希望有人能指出我正确的方向)

我有一个5列的数据框,不是很宽。当我想在控制台中显示数据框的顶部时,我只得到第一行和最后一行,而在中间它只显示“...”

这发生在我:

  • 在控制台中调用print命令
  • 从脚本调用print
  • 使用print(df[0:5])
  • 使用df.head()

如果我只打印df[0:1],它会显示中间列。 (我在我的帖子中调用df,但我在代码中称它为channeldf

例子:

channeldf.head()
Out[90]: 
           IDs        ...                       y2
0  "1170910_1"        ...               413915.163
1  "1170910_2"        ...         413916.485731237
2  "1170910_3"        ...         413914.945323079
3  "1170910_4"        ...         413904.985148227
4  "1170910_5"        ...         413897.477030875
[5 rows x 5 columns]

channeldf[0:5]
Out[92]: 
           IDs        ...                       y2
0  "1170910_1"        ...               413915.163
1  "1170910_2"        ...         413916.485731237
2  "1170910_3"        ...         413914.945323079
3  "1170910_4"        ...         413904.985148227
4  "1170910_5"        ...         413897.477030875
[5 rows x 5 columns]

channeldf[0:1]
Out[91]: 
           IDs          x1         y1          x2          y2
0  "1170910_1"  144923.193  413914.78  144919.756  413915.163

我找到了另一个问题,展示了如何调整Pandas所需的宽度,但这并没有影响我的问题。我试着让它变得非常狭窄,然后真的很宽:

pd.set_option('display.width',100)
print(channeldf.iloc[1:2,:])
           IDs          x1        ...                       x2                y2
1  "1170910_2"  144919.756        ...         144915.931907037  413916.485731237
[1 rows x 5 columns]
print(channeldf.iloc[0:1,:])
           IDs          x1         y1          x2          y2
0  "1170910_1"  144923.193  413914.78  144919.756  413915.163
print(channeldf.iloc[1:3,:])
           IDs        ...                       y2
1  "1170910_2"        ...         413916.485731237
2  "1170910_3"        ...         413914.945323079
[2 rows x 5 columns]
pd.set_option('display.width',1000)
print(channeldf.iloc[1:2,:])
           IDs          x1        ...                       x2                y2
1  "1170910_2"  144919.756        ...         144915.931907037  413916.485731237
[1 rows x 5 columns]
print(channeldf.iloc[1:3,:])
           IDs        ...                       y2
1  "1170910_2"        ...         413916.485731237
2  "1170910_3"        ...         413914.945323079

我的屏幕比这个宽得多;即使是我的例子中最宽的输出也只是可用宽度的60%。我在查找好的数据片段时遇到了麻烦,但列是ID,x1,y1,x2,y2,最后4列包含几乎恒定的数字坐标。第一行有2或3位数字,而所有其他行有9位数字。

我希望有人能给出解决方案或指出我正确的方向。如果有什么我可以清理的;我很乐意这样做(但请告诉我如何到达那里)

python pandas dataframe pycharm
1个回答
0
投票

Pandas为显示器进行了大量的输出格式化/折叠,这是处理大型数据集的基本功能。

这个现象的一个很好的可重现的例子是python解释器(Jupyter和其他环境可能会为你做额外的事情):

import pandas as pd
df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice', 'Doug'],'education':['hs diploma', 'advanced degree', 'four year degree', 'middle school'], 'income': [40000, 50000, 42000,20000]})

看看DataFrame

df
          education  income   user
0        hs diploma   40000    Bob
1   advanced degree   50000   Jane
2  four year degree   42000  Alice
3     middle school   20000   Doug

仅显示两个外部列

pd.set_option("display.max_columns", 2)
df
          education  ...     user
0        hs diploma  ...      Bob
1   advanced degree  ...     Jane
2  four year degree  ...    Alice
3     middle school  ...     Doug

[4 rows x 3 columns]

请注意输出df尺寸:3列,中间折叠。

pd.set_option("display.max_columns", 0)

令人惊讶的是打印所有,以及更好的None值。

将DataFrame转储为CSV并使用电子表格编辑器加载有时是呈现结果数据的好方法。

© www.soinside.com 2019 - 2024. All rights reserved.