簡體   English   中英

IPython / Jupyter:如何自定義顯示 function

[英]IPython / Jupyter: how to customize the display function

在 IPython / JupyterLab 中顯示內容時,我遇到了 3 個基本問題。

(1) 我有一個 pandas dataframe 有很多列。 首先,我確保我可以看到其中的一部分:

import numpy as np
import pandas as pd
np.set_printoptions(linewidth=240,edgeitems=5,precision=3)
pd.set_option('display.width',1800) #number of pixels of the output
pd.set_option('display.max_columns',100) #replace the number with None to print all columns
pd.set_option('display.max_rows',10) #max_columns/max_rows sets the maximum number of columns/rows displayed when a frame is pretty-printed
pd.set_option('display.min_rows',9) #once max_rows is exceeded, min_rows determines how many rows are shown in the truncated representation
pd.set_option('display.precision',3) #number of digits in the printed float number

如果我打印它,一切都會混在一起:在此處輸入圖像描述是否可以打印寬文本,即每行(即使更長)僅打印在 output 中的一行上,當行更寬時使用 slider比 window?

(2) 如果我顯示提到的 dataframe,它看起來非常漂亮(有一個滑塊),但是一些字符串條目顯示超過 4 行:在此處輸入圖像描述

如何確保每個條目都顯示在 1 行中

(3) 下面的代碼生成 output,它工作正常:

import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0.1,30,1000); 
fig,ax=plt.subplots(1, 4, constrained_layout=True, figsize=[15,2])
ax=ax.ravel()
ax[0].plot( x, np.sin(x))
ax[1].plot( x, np.log(1+x))
ax[2].plot( x, np.sin(30/x))
ax[3].plot( x, np.sin(x)*np.sin(2*x))
plt.show()

在此處輸入圖像描述但是,當我將[15,2]更改為[35,2]時,該圖將僅與 window 一樣寬。 我怎樣才能實現更大的寬度產生 slider (如數據框的顯示),以便我可以使圖像盡可能寬?

您已經通過決定使用 (2) 中的方法顯示 dataframe 解決了 (1)。 在我看來,使用print顯示 dataframe 並不是很有用。

(2): display(df)自動利用空格來包裝單元格內容。 我沒有找到 pandas 選項來禁用此行為。 幸運的是,其他人已經遇到了同樣的問題,並且另一個人提供了解決方案

您必須更改 dataframe 的樣式屬性。 我做了一個簡短的示例,您可以從中復制該行:

import pandas as pd

# Construct data frame content
long_text = 'abcabcabc ' * 12
long_row = [long_text for _ in range(45)]
long_table = [long_row for _ in range(12)]

df = pd.DataFrame(long_table)

# Change style of white spaces
df = df.style.set_properties(**{'white-space': 'nowrap'})

# Display dataframe
display(df)

Output: 圖片

(3) 正如我在評論中已經提到的,您只需雙擊圖表,它就會顯示為 slider。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM