4
如何在顯示數據框等時限制Pandas內的列寬?我知道display.max_colwidth
,但它不影響列名稱。另外,我不想打破名字,而是要截斷。如何限制Pandas中列標題的寬度
如果我設置了display.max_colwidth
它肯定會限制這些值,並用省略號將它們縮短,但列名仍然很長且不受影響。我在documentation的任何地方都沒有看到這一點,並沒有在大線索here或here中看到它。這是一個錯誤嗎?我知道我可以截取列的名稱,但我希望它們能夠很長時間,否則顯示時縮短。
只是要清楚,這是發生了什麼事:
以前
State area_harveste_2016_1000_acres area_harvested_2017_1000_acres yield_per_acr_2016_bushels yield_per_acre_2017_bushels
4 Alabama 315 235 120.0 165.0
改變寬度
pd.set_option("display.max_colwidth",5)
後
State area_harveste_2016_1000_acres area_harvested_2017_1000_acres yield_per_acr_2016_bushels yield_per_acre_2017_bushels
4 A... 315 235 1... 1...
更新
目前,這是與大熊貓確認的問題(如20.3與問題#7059和#16911。在解決之前,我寫了一個解決方法,它設置並取消設置max_colwidth
以及重命名截斷。我不能做雖然它沒有答案,從這個similar question
def pf(df, L=15):
"""Limit ENTIRE column width (including header)"""
O = pd.get_option("display.max_colwidth")
pd.set_option("display.max_colwidth", L)
print(df.rename(columns=lambda x: x[:L - 3] + '...' if len(x) > L else x))
pd.set_option("display.max_colwidth", O)
可能重複[pandas顯示:截斷列顯示而不是包裝](https://stackoverflow.com/questions/45043968/pandas-display-truncate-column-display-rather-than-wrapping) –
@BradSolomon - 這些答案當然提供了一些解決方法 - 不是實際的解決方案,當我環顧四周時,帖子沒有出現 - 現在我知道其他人也在爭鬥,我在GitHub上發現了兩張關於此的票 - 如果其他人也想跟蹤: [#7059](https://github.com/pandas-dev/pandas/issues/7059)和[#16911](https://github.com/pandas-dev/pandas/issues/16911) – ehiller