【问题标题】:Why doesn't my python pandas code shows a table?为什么我的 python pandas 代码不显示表格?
【发布时间】:2021-04-15 15:01:56
【问题描述】:

我正在使用 Spyder 和以下源代码:

import pandas as pd


filename = "file.csv" # 5.35 GB in size

df = pd.read_csv(filename, nrows=5)
pd.set_option('display.max_columns', None)

df
    

输出

runfile('C:/Users/pc/Desktop/Data Mining/file.py', wdir='C:/Users/pc/Desktop/Data Mining/')
Reloaded modules: jupyter_client.session, zmq.eventloop, zmq.eventloop.ioloop, 
tornado.platform, tornado.platform.asyncio, tornado.gen, zmq.eventloop.zmqstream, 
jupyter_client.jsonutil, jupyter_client.adapter, spyder, spyder.pil_patch, PIL, 
PIL._version, PIL.Image, PIL.ImageMode, PIL.TiffTags, PIL._binary, PIL._util, PIL._imaging, 
cffi, cffi.api, cffi.lock, cffi.error, cffi.model

为什么我的 python pandas 代码不显示表格?


另外,使用 print(df) 后:

import pandas as pd


filename = "clash_royale_games_Jan_2019.csv"


df = pd.read_csv(filename, nrows=5)
pd.set_option('display.max_columns', None)
print(df)

输出

runfile('C:/Users/pc/Desktop/Data Mining/file.py', wdir='C:/Users/pc/Desktop/Data Mining/')
Reloaded modules: jupyter_client.session, zmq.eventloop, zmq.eventloop.ioloop, tornado.platform, tornado.platform.asyncio, tornado.gen, zmq.eventloop.zmqstream, jupyter_client.jsonutil, jupyter_client.adapter, spyder, spyder.pil_patch, PIL, PIL._version, PIL.Image, PIL.ImageMode, PIL.TiffTags, PIL._binary, PIL._util, PIL._imaging, cffi, cffi.api, cffi.lock, cffi.error, cffi.model
              timestamp  arena_id  has_won        tag  \
0  2018-12-31T23:00:46Z        16        1  289VVL288   
1  2018-12-31T23:01:34Z        14        1  2G0PRYVRU   
2  2018-12-31T23:02:00Z        13        1   92GLVGQU   
3  2018-12-31T23:02:00Z        14        1  2Y28RPJRC   
4  2018-12-31T23:02:02Z        16        0    8RYLV2R   

                                         player_deck  
0  elixir-collector_golem_mega-knight_mega-minion...  
1  dark-prince_goblin-gang_miner_pekka_the-log_to...  
2  baby-dragon_balloon_barbarian-barrel_bowler_fr...  
3  baby-dragon_fireball_inferno-dragon_mega-minio...  
4  archers_barbarian-barrel_goblins_hog-rider_kni...  

您会看到第 5 列被截断。

如何完整显示第 5 列?

【问题讨论】:

  • 你必须使用spyder吗? jupyter notebooks 很容易解决这个问题 - 你可以设置列宽等。(公平地说,这可能不是使用 jupyter 的功能;我只使用过没有这个问题的 jupyter 或 vs-code)

标签: python pandas data-analysis


【解决方案1】:

pandas 显示选项在pd.options.display 中。将 pd.options.display.max_colwidth 和可能的 pd.options.display.width 设置为更高的值应该可以解决截断问题。

您的第一个块没有显示任何内容,因为您没有告诉它。 IPython 和 Jupyter 等工具将显示单元格中最后一个对象的 __repr__,但这是特定于这些平台的,Spyder 基本上只是将您的文件作为常规 Python 脚本运行。

【讨论】:

    猜你喜欢
    • 2013-12-03
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    相关资源
    最近更新 更多