欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python Pandas head tail显示全部行或列

程序员文章站 2024-01-05 17:14:52
...

pandas做数据处理、数据分析时候常会用到head()、tail()两个方法,两个方法默认会显示前/后5行,并且会默认忽略某些列。

如果想展示全部列的内容,很简单,加一行搞定。

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

来看一段完整的代码,猫哥以经典的分析titainic数据为例,简介一下:

import pandas as pd

train_df = pd.read_csv('titainic.csv')
print(train_df.head())
pd.set_option('display.max_columns', None)
print(train_df.head())

设置前后,分别对一下结果,看起来这样的:

D:\workspace\pyprojects\titanic_g\tPrac>python dataanaly.py
   PassengerId  Survived  Pclass  ...     Fare Cabin  Embarked
0            1         0       3  ...   7.2500   NaN         S
1            2         1       1  ...  71.2833   C85         C
2            3         1       3  ...   7.9250   NaN         S
3            4         1       1  ...  53.1000  C123         S
4            5         0       3  ...   8.0500   NaN         S

[5 rows x 12 columns]
   PassengerId  Survived  Pclass  \
0            1         0       3
1            2         1       1
2            3         1       3
3            4         1       1
4            5         0       3

                                                Name     Sex   Age  SibSp  \
0                            Braund, Mr. Owen Harris    male  22.0      1
1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1
2                             Heikkinen, Miss. Laina  female  26.0      0
3       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1
4                           Allen, Mr. William Henry    male  35.0      0

   Parch            Ticket     Fare Cabin Embarked
0      0         A/5 21171   7.2500   NaN        S
1      0          PC 17599  71.2833   C85        C
2      0  STON/O2. 3101282   7.9250   NaN        S
3      0            113803  53.1000  C123        S
4      0            373450   8.0500   NaN        S

换行了,有点儿视觉上不爽,但是毕竟显示全了吧。

 

同理,设置全部行也很容易,一句话的事情:

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

简单!

相关标签: Python