[英]Display “long” pandas dataframe in jupyter notebook with “foldover” (wrapping)?
Let's say I have a pandas dataframe with many columns: 假设我有一个包含许多列的pandas数据框:
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":
但是,这有点不方便,我想知道是否有一种优雅的方式来显示“折叠”表:
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? 我想知道是否有办法更干净地做上述事情,可能是通过改变熊猫或jupyter设置?
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
In addition to setting max cols like you did, I'm importing display
除了像你一样设置max cols,我还要导入
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 创建一个框架,然后一个简单的for循环显示每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 编辑:忘了添加输出图片
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.