【问题标题】:Pandas not showing all the columns I asked for, Why?Pandas 没有显示我要求的所有列,为什么?
【发布时间】:2022-12-09 14:47:30
【问题描述】:

我是大熊猫的新手。我正在处理的文件非常大,我只想从 9 列中读取几行。但是,当我运行代码时,它只显示 9 列中的 3 列。我不知道为什么。

columns = [0, 1, 2, 3, 4, 5, 10, 12, 17]
header_names = ['Incident Number', 'Incident Type', 'Report Date', 'Nearest Populated Center',
                'Province', 'Company', 'Substance', 'Significant', 'What Happened category']
p_line = pd.read_csv(r"pipeline_data.csv",
                     skiprows=1,
                     nrows=5,
                     usecols=columns,
                     names=header_names,
                     index_col=0)

这是输出... 另外,为什么不打印事件编号?

                                                    Incident Type  ...    What Happened  category
Incident Number                                                     ...                          
INC2007-097                                   Release of Substance  ...    Corrosion and Cracking
INC2008-001                                   Release of Substance  ...    Corrosion and Cracking
INC2008-004                                   Release of Substance  ...         Equipment Failure
INC2008-008                                   Release of Substance  ...  Defect and Deterioration
INC2008-009      Release of Substance, Adverse Environmental Ef...  ...         Equipment Failure

[5 rows x 8 columns]

然后我补充说

pd.set_option('display.max_columns', None)

但是输出随后被分解了....

                                                     Incident Type  \
Incident Number                                                      
INC2007-097                                   Release of Substance   
INC2008-001                                   Release of Substance   
INC2008-004                                   Release of Substance   
INC2008-008                                   Release of Substance   
INC2008-009      Release of Substance, Adverse Environmental Ef...   

                Report Date Nearest Populated Center          Province  \
Incident Number                                                          
INC2007-097      01/02/2008           Grande Prairie           Alberta   
INC2008-001      01/02/2008                   Cromer          Manitoba   
INC2008-004      01/23/2008                   Cromer          Manitoba   
INC2008-008      01/29/2008              Fort Nelson  British Columbia   
INC2008-009      01/29/2008                   Taylor  British Columbia   

                                                           Company  \
Incident Number                                                      
INC2007-097                                 Alliance Pipeline Ltd.   
INC2008-001                                Enbridge Pipelines Inc.   
INC2008-004                                Enbridge Pipelines Inc.   
INC2008-008      Westcoast Energy Inc., carrying on business as...   
INC2008-009      Westcoast Energy Inc., carrying on business as...   

                                              Substance Significant  \
Incident Number                                                       
INC2007-097                         Natural Gas - Sweet          No   
INC2008-001                           Crude Oil - Sweet          No   
INC2008-004                           Crude Oil - Sweet          No   
INC2008-008                         Natural Gas - Sweet          No   
INC2008-009      Potassium Hydroxide (caustic solution)         Yes   

                   What Happened category  
Incident Number                            
INC2007-097        Corrosion and Cracking  
INC2008-001        Corrosion and Cracking  
INC2008-004             Equipment Failure  
INC2008-008      Defect and Deterioration  
INC2008-009             Equipment Failure  

我只想要一张长表中的所有信息,而不是一些奇怪的破碎表

【问题讨论】:

    标签: pandas csv


    【解决方案1】:

    也许您可以使用此功能并在新选项卡中查看您的表格

    df.to_html('data.html')
    

    【讨论】:

      【解决方案2】:

      您可以使用以下配置选项:

      • pd.options.display.max_columns = None

        它设置当框架打印精美并且截断的行被省略号替换时显示的最大列数。

      • pd.options.display.max_colwidth = None

        它设置列的最大宽度。此长度或更长的单元格将被省略号截断。

      • pd.options.display.width = 1000

        您将不得不试验这个值。它设置显示的字符数。

      其他配置选项解释here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-12
        相关资源
        最近更新 更多