用“foldover”(包装)在jupyter笔记本中显示“long”pandas数据框?

2024-09-30 08:37:46 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个包含许多列的pandas数据帧:

enter image description here

我可以通过向左/向右滚动查看所有列。但是,这有点不方便,我想知道是否有一种优雅的方式来显示带有“foldover”的表:

enter image description here

为了生成上述内容,我手动将数据帧分割成块并显示每个块(这就是为什么/etc的间距不完美)。在

我想知道是否有一种方法可以更干净地完成上述操作,比如改变熊猫或jupyter的设置?在


Tags: 数据方法内容pandas方式etcjupyter手动
2条回答

试试这个:

In [135]: df = pd.DataFrame(np.random.rand(3, 30), columns=list(range(30)))

In [136]: pd.options.display.expand_frame_repr=True

In [137]: pd.options.display.max_columns = None

In [138]: pd.options.display.width = 80

In [139]: df
Out[139]:
         0         1         2         3         4         5         6   \
0  0.072370  0.388609  0.112033  0.829140  0.700152  0.645331  0.063483
1  0.890765  0.330274  0.900561  0.128318  0.056443  0.239560  0.568522
2  0.295088  0.101399  0.417066  0.657503  0.872717  0.153140  0.909876

         7         8         9         10        11        12        13  \
0  0.497130  0.852824  0.778126  0.710167  0.526406  0.416188  0.154483
1  0.451316  0.409711  0.352989  0.810885  0.540569  0.999328  0.144429
2  0.442140  0.892209  0.150371  0.337189  0.584538  0.152138  0.278306

         14        15        16        17        18        19        20  \
0  0.520901  0.857807  0.969782  0.577220  0.016009  0.809139  0.231900
1  0.561181  0.446312  0.468740  0.076465  0.383884  0.850491  0.815509
2  0.147742  0.957585  0.010312  0.021724  0.572048  0.952412  0.033100

         21        22        23        24        25        26        27  \
0  0.656393  0.823157  0.507222  0.889953  0.076415  0.820150  0.441099
1  0.919607  0.942032  0.586774  0.469604  0.596542  0.156348  0.099294
2  0.978045  0.537873  0.283019  0.582568  0.012389  0.943704  0.028346

         28        29
0  0.921219  0.569421
1  0.016056  0.298474
2  0.061831  0.488659

除了像您一样设置max cols之外,我还导入了display

import pandas as pd
pd.set_option('display.max_columns', None)

from IPython.display import display

创建一个帧,然后创建一个简单的for循环,每隔30列显示一次

^{2}$

编辑:忘记添加输出的图片

enter image description here

相关问题 更多 >

    热门问题