killajoule
killajoule

Reputation: 3832

Display "long" pandas dataframe in jupyter notebook with "foldover" (wrapping)?

Let's say I have a pandas dataframe with many columns:

enter image description here

I can view all of the columns by scrolling left/right. However, this is a bit inconvenient and I was wondering if there was an elegant way to display the table with "foldover":

enter image description here

To generate the above, I manually chopped up the dataframe into chunks and displayed each chunk (which is why the spacing/etc is not perfect).

I was wondering if there was a way to do something like the above more cleanly, possibly by changing pandas or jupyter settings?

Upvotes: 4

Views: 5020

Answers (2)

Bob Haffner
Bob Haffner

Reputation: 8483

In addition to setting max cols like you did, I'm importing display

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

from IPython.display import display

creating a frame then a simple for loop to display every 30 cols

df = pd.DataFrame([range(200)])

cols = df.shape[1]

for i in range(0,cols,30):   
    display(df.iloc[:,i:i+30])

EDIT: Forgot to add a pic of the output

enter image description here

Upvotes: 4

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210842

try this:

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

Upvotes: 2

Related Questions