如何使用 Pandas 顯示 DataFrame 中的所有行?
Pandas 是 Python 中一個強大且流行的資料處理庫,它提供了一種靈活且高效的方式來處理和分析資料。Pandas 的關鍵特性之一是它的 DataFrame 物件,它是一個二維表格資料結構,類似於電子表格或 SQL 表格。
在 Jupyter notebook 或 Python 控制檯中直接列印 Pandas DataFrame 時,如果 DataFrame 具有許多行,它會自動截斷顯示輸出。預設情況下,僅顯示有限數量的行和列,以確保輸出簡潔易讀。可以透過修改 Pandas 模組的 max_rows 和 max_columns 選項來更改此預設行為。
使用 to_string() 或 to_markdown() 函式顯示所有行
在 pandas 中,to_string() 函式用於顯示完整的 DataFrame,即使它包含大量超過最大顯示限制的行或列。預設情況下,當 DataFrame 包含的行或列多於指定的最大顯示限制時,pandas 會截斷 DataFrame 的輸出。但是,to_string() 函式允許顯示 DataFrame 的所有行,無論其數量如何。
Pandas 中的 to_markdown() 函式可用於將 DataFrame 轉換為 Markdown 格式的表格,該表格可以輕鬆地在 Jupyter notebook、網站或 README 檔案中顯示。生成的表格易於閱讀和理解,因為標題和值用管道分隔並對齊以提高可讀性。此外,to_markdown() 函式支援多個選項,允許自定義輸出,例如隱藏索引、指定對齊方式和自定義表標題。
語法
要使用 Pandas 顯示 DataFrame 中的所有行,您需要遵循以下語法:
print(pandas.dataFrameName.to_string()) OR print(pandas.dataFrameName.to_markdown())
要使用 to_string() 函式,只需在 DataFrame 上呼叫它並使用 print() 函式顯示生成的文字即可。此方法允許自定義輸出,包括設定要顯示的最大行數、指定列寬以及選擇如何顯示缺失值。
要使用 to_markdown() 函式,只需在 DataFrame 上呼叫它並使用 print() 函式顯示生成的文字即可。此方法也以更好的格式顯示所有行。
示例
此程式碼將 CSV 資料匯入名為 cricketers_df 的 Pandas DataFrame,其中包含 20 位退役板球運動員的詳細資訊。DataFrame 的每一行代表一位板球運動員,幷包含他們的姓名、國家、首次亮相年份和退役年份。
最後,使用 to_string() 方法將整個 DataFrame 列印到控制檯。此方法將 DataFrame 轉換為可以列印到控制檯的字串表示形式。由於使用了 to_string() 方法,在這裡我們可以看到 DataFrame 的所有行。
import pandas as pd
cricketers_df = pd.read_csv('cricketers.csv')
print(cricketers_df.to_string())
輸出
Name Country Debut Year Retirement Year 0 Sachin Tendulkar India 1989 2013 1 Brian Lara West Indies 1990 2007 2 Ricky Ponting Australia 1995 2012 3 Jacques Kallis South Africa 1995 2014 4 Inzamam-ul-Haq Pakistan 1991 2007 5 Sanath Jayasuriya Sri Lanka 1989 2011 6 Muttiah Muralitharan Sri Lanka 1992 2010 7 Shane Warne Australia 1992 2007 8 Glenn McGrath Australia 1993 2007 9 Anil Kumble India 1990 2008 10 Sourav Ganguly India 1992 2008 11 Rahul Dravid India 1996 2012 12 VVS Laxman India 1996 2012 13 Adam Gilchrist Australia 1996 2008 14 Matthew Hayden Australia 1994 2009 15 Mark Waugh Australia 1991 2002 16 Steve Waugh Australia 1985 2004 17 Michael Vaughan England 1999 2009 18 Andrew Flintoff England 1998 2009 19 Marcus Trescothick England 2000 2008 20 Kevin Pietersen England 2004 2018
示例
在這個例子中,我們使用了相同的資料,並從預先存在的 CSV 檔案中讀取資料。這次,我們使用 DataFrame 上的 to_markdown() 方法列印所有資料。
import pandas as pd
cricketers_df = pd.read_csv('cricketers.csv')
print(cricketers_df.to_markdown())
輸出
| | Name | Country | Debut Year | Retirement Year | |---:|:---------------------|:-------------|-------------:|------------------:| | 0 | Sachin Tendulkar | India | 1989 | 2013 | | 1 | Brian Lara | West Indies | 1990 | 2007 | | 2 | Ricky Ponting | Australia | 1995 | 2012 | | 3 | Jacques Kallis | South Africa | 1995 | 2014 | | 4 | Inzamam-ul-Haq | Pakistan | 1991 | 2007 | | 5 | Sanath Jayasuriya | Sri Lanka | 1989 | 2011 | | 6 | Muttiah Muralitharan | Sri Lanka | 1992 | 2010 | | 7 | Shane Warne | Australia | 1992 | 2007 | | 8 | Glenn McGrath | Australia | 1993 | 2007 | | 9 | Anil Kumble | India | 1990 | 2008 | | 10 | Sourav Ganguly | India | 1992 | 2008 | | 11 | Rahul Dravid | India | 1996 | 2012 | | 12 | VVS Laxman | India | 1996 | 2012 | | 13 | Adam Gilchrist | Australia | 1996 | 2008 | | 14 | Matthew Hayden | Australia | 1994 | 2009 | | 15 | Mark Waugh | Australia | 1991 | 2002 | | 16 | Steve Waugh | Australia | 1985 | 2004 | | 17 | Michael Vaughan | England | 1999 | 2009 | | 18 | Andrew Flintoff | England | 1998 | 2009 | | 19 | Marcus Trescothick | England | 2000 | 2008 | | 20 | Kevin Pietersen | England | 2004 | 2018 |
我們學習瞭如何使用 Pandas 中的 to_string() 和 to_markdown() 函式顯示 DataFrame 中的所有行。to_string() 函式是處理大型 DataFrame 的有用工具,因為它允許輕鬆視覺化和分析 DataFrame 中的所有資料。
與僅將 DataFrame 作為字串輸出的 to_string() 不同,to_markdown() 輸出一個可用於文件的 markdown 表格。生成的表格易於閱讀且外觀專業。
更改預設列印設定以顯示所有行
Pandas 的 option_context() 函式允許在指定的上下文中臨時修改 DataFrame 的顯示選項。使用此函式,我們可以更改各種顯示選項,例如要顯示的最大行數和列數、每列的顯示寬度以及浮點值的精度。使用 option_context(),我們可以臨時修改顯示選項以檢視所有行或列,而無需永久更改選項。
在 Pandas 中,set_option() 是一種允許您設定資料顯示方式的不同選項的方法。一個有用的選項是 display.max_rows 選項,它控制在 DataFrame 中顯示的最大行數。透過將此值設定為 None,您可以顯示 DataFrame 中的所有行,從而更輕鬆地檢視和分析資料。
語法
要透過更改預設設定使用 Pandas 顯示 DataFrame 中的所有行,您需要遵循以下語法
with pandas.option_context('display.max_rows', None,):
print(dataframeName)
提供的語法將顯示選項設定為顯示名為“dataframeName”的 Pandas DataFrame 中的所有行。option_context() 函式臨時將可顯示的最大行數設定為 None,這意味著列印 DataFrame 時將顯示 DataFrame 中的所有行。
pandas.set_option('display.max_rows', None)
print(dataframeName)
要使用 set_option(),只需傳入您要設定的選項及其值,例如:pd.set_option('display.max_rows', None)。設定選項後,任何後續的 DataFrame 輸出都將反映您所做的更改。此函式所做的更改是永久性的,直到再次更改。
示例
此程式碼讀取 CSV 檔案 fruits.csv,並將資料儲存在 fruits_df 中,其中包含有關各種水果的資料,包括它們的名稱、顏色、重量和價格。DataFrame 包含 15 個條目,每個條目代表一種不同的水果。
然後,程式碼使用 option_context() 函式臨時將 DataFrame 中要顯示的最大行數設定為 None。這意味著列印 DataFrame 時將顯示 DataFrame 的所有行,而不僅僅是摘要。
最後,程式碼使用 print(fruits_df) 列印 fruits_df DataFrame,由於 option_context() 設定,它將顯示所有 15 行。
import pandas as pd
fruits_df = pd.read_csv('fruits.csv')
with pd.option_context('display.max_rows', None,):
print(fruits_df)
輸出
Name Color Weight (oz) Price ($) 0 Apple Red 4.0 0.50 1 Orange Orange 6.0 0.40 2 Banana Yellow 5.0 0.20 3 Grapes Green 3.0 0.30 4 Pineapple Brown 16.0 1.50 5 Strawberry Red 1.0 0.10 6 Watermelon Green 128.0 2.00 7 Kiwi Brown 3.0 0.30 8 Mango Orange 8.0 1.00 9 Pear Green 5.0 0.40 10 Peach Orange 6.0 0.50 11 Plum Purple 4.0 0.40 12 Cherry Red 0.5 0.10 13 Blueberry Blue 0.3 0.05 14 Raspberry Red 0.2 0.05
示例
在這個例子中,我們透過從 CSV 檔案讀取資料使用了相同的水果資料。然後,我們使用 set_option() 更改預設設定並列印所有資料。
import pandas as pd
fruits_df = pd.read_csv('fruits.csv')
pd.set_option('display.max_rows', None)
print(fruits_df)
輸出
Name Color Weight (oz) Price ($) 0 Apple Red 4.0 0.50 1 Orange Orange 6.0 0.40 2 Banana Yellow 5.0 0.20 3 Grapes Green 3.0 0.30 4 Pineapple Brown 16.0 1.50 5 Strawberry Red 1.0 0.10 6 Watermelon Green 128.0 2.00 7 Kiwi Brown 3.0 0.30 8 Mango Orange 8.0 1.00 9 Pear Green 5.0 0.40 10 Peach Orange 6.0 0.50 11 Plum Purple 4.0 0.40 12 Cherry Red 0.5 0.10 13 Blueberry Blue 0.3 0.05 14 Raspberry Red 0.2 0.05
結論
我們學習瞭如何透過更改 Pandas 中的預設列印設定來顯示 DataFrame 中的所有行。option_context() 函式有助於提高 DataFrame 的可讀性和可用性,並使分析大型資料集更容易。修改後的顯示選項僅在指定的上下文中有效,並且在上下文退出後返回原始設定。
set_option() 方法與 option_context() 一樣有用,但唯一的缺點是它會永久更改列印設定,直到再次更改。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP