本文翻譯自:Pretty-print an entire Pandas Series / DataFrame
I work with Series and DataFrames on the terminal a lot. 我在終端上經常使用Series和DataFrames。 The default __repr__
for a Series returns a reduced sample, with some head and tail values, but the rest missing. 系列的默認__repr__
返回減少的樣本,具有一些頭和尾值,但其餘部分丟失。
Is there a builtin way to pretty-print the entire Series / DataFrame? 有沒有一種內置方法可以漂亮地打印整個Series / DataFrame? Ideally, it would support proper alignment, perhaps borders between columns, and maybe even color-coding for the different columns. 理想情況下,它將支持適當的對齊方式,可能支持列之間的邊界,甚至可能對不同列進行顏色編碼。
#1樓
參考:https://stackoom.com/question/1IFBJ/漂亮地打印整個Pandas系列-DataFrame
#2樓
Sure, if this comes up a lot, make a function like this one. 當然,如果出現很多情況,請創建一個像這樣的功能。 You can even configure it to load every time you start IPython: https://ipython.org/ipython-doc/1/config/overview.html 您甚至可以將其配置爲在每次啓動IPython時加載: https : //ipython.org/ipython-doc/1/config/overview.html
def print_full(x):
pd.set_option('display.max_rows', len(x))
print(x)
pd.reset_option('display.max_rows')
As for coloring, getting too elaborate with colors sounds counterproductive to me, but I agree something like bootstrap's .table-striped
would be nice. 至於顏色,過於精緻的顏色聽起來適得其反,但我同意類似bootstrap的.table-striped
這樣的東西會很好。 You could always create an issue to suggest this feature. 您總是可以創建一個問題來建議該功能。
#3樓
You can also use the option_context
, with one or more options: 您還可以將option_context
與一個或多個選項一起使用:
with pd.option_context('display.max_rows', None, 'display.max_columns', None): # more options can be specified also
print(df)
This will automatically return the options to their previous values. 這將使選項自動返回其先前的值。
If you are working on jupyter-notebook, using display(df)
instead of print(df)
will use jupyter rich display logic (like so) . 如果您正在使用jupyter-notebook,則使用display(df)
而不是print(df)
將使用jupyter-rich顯示邏輯(像這樣) 。
#4樓
After importing pandas, as an alternative to using the context manager, set such options for displaying entire dataframes: 導入熊貓後,作爲使用上下文管理器的替代方法,請設置以下選項以顯示整個數據框:
pd.set_option('display.max_columns', None) # or 1000
pd.set_option('display.max_rows', None) # or 1000
pd.set_option('display.max_colwidth', -1) # or 199
For full list of useful options, see: 有關有用選項的完整列表,請參見:
pd.describe_option('display')
#5樓
No need to hack settings. 無需修改設置。 There is a simple way: 有一個簡單的方法:
print(df.to_string())
#6樓
Try this 嘗試這個
pd.set_option('display.height',1000)
pd.set_option('display.max_rows',500)
pd.set_option('display.max_columns',500)
pd.set_option('display.width',1000)