获取pandas数据框架或序列的头部和尾部(附代码)

447 阅读4分钟

在这个Python教程中,我们将讨论获得pandas DataFrame或Series对象头部和尾部的不同方法。所以让我们开始吧。


为什么要获取pandas DataFrame或Series的头部和尾部?

我们都知道,**Pandas**是一个重要的Python库,被广泛用于数据分析。而众所周知,数据分析要处理非常大的数据集。因此,为了快速了解大型样本数据集(以pandas DataFrame或Series对象的形式加载),我们需要pandas DataFrame或Series的头和尾。

我们主要使用pandas DataFrame类的DataFrame.head()DataFrame.tail() 函数来分别获得pandas DataFrame或Series的第一和最后的N 行(默认情况下,这个N=5)。

pandas DataFrame的头部和尾部

所以,在继续讨论pandas DataFrame对象的头部和尾部之前,让我们先创建一个样本pandas DataFrame对象。

创建一个样本的pandas DataFrame对象

# Import pandas Python module
import pandas as pd

# Create a large pandas DataFrame object
df = pd.DataFrame({'RegNo': [111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125],
                   'Dept': ['ECE', 'ICE', 'IT', 'CSE', 'CHE', 'EE', 'ME', 'CSE', 'ICE', 'TT', 'ECE', 'IT', 'ME', 'BT', 'EE']})

# Print the created pandas DataFrame
print('Sample pandas DataFrame:\n')
print(df)

输出

Sample pandas DataFrame:

    RegNo Dept
0     111  ECE
1     112  ICE
2     113   IT
3     114  CSE
4     115  CHE
5     116   EE
6     117   ME
7     118  CSE
8     119  ICE
9     120   TT
10    121  ECE
11    122   IT
12    123   ME
13    124   BT
14    125   EE

获取pandas DataFrame的头部: pandas.DataFrame.head()

# Get the head of the sample pandas Series
print('First 10 rows of the sample pandas DataFrame:\n')
temp_df = df.head(10)
print(temp_df)

输出

First 10 rows of the sample pandas DataFrame:

   RegNo Dept
0    111  ECE
1    112  ICE
2    113   IT
3    114  CSE
4    115  CHE
5    116   EE
6    117   ME
7    118  CSE
8    119  ICE
9    120   TT

获取pandas DataFrame的尾部:pandas.DataFrame.tail()

# Get the tail of the sample pandas Series
print('Last 10 rows of the sample pandas DataFrame:\n')
temp_df = df.tail(10)
print(temp_df)

输出

Last 10 rows of the sample pandas DataFrame:

    RegNo Dept
5     116   EE
6     117   ME
7     118  CSE
8     119  ICE
9     120   TT
10    121  ECE
11    122   IT
12    123   ME
13    124   BT
14    125   EE

获取pandas DataFrame的头部和尾部:pandas.option_context()

# Get the head and tail of the sample pandas DataFrame
# Using the pd.option_context() function in Pandas
print('First and Last 5 rows of the sample pandas DataFrame:\n')
with pd.option_context('display.max_rows',10):
    print(df)

输出

First and Last 5 rows of the sample pandas DataFrame:

    RegNo Dept
0     111  ECE
1     112  ICE
2     113   IT
3     114  CSE
4     115  CHE
..    ...  ...
10    121  ECE
11    122   IT
12    123   ME
13    124   BT
14    125   EE

[15 rows x 2 columns]

一个pandas系列的头部和尾部

所以,在继续讨论pandas系列对象的头部和尾部之前,让我们先创建一个pandas系列对象的例子。

创建一个示例的pandas系列对象

# Import pandas Python module
import pandas as pd
# Import NumPy Python module
import numpy as np

# Create a pandas Series
sr = pd.Series(np.random.randn(1000))

# Print the created pandas Series
print('Sample pandas Series:\n')
print(sr)

输出

Sample pandas Series:

0     -0.157775
1     -0.108095
2     -0.876501
3     -0.591994
4     -0.435755
         ...   
995    1.186127
996   -0.898278
997   -0.267392
998    1.295608
999   -2.024564
Length: 1000, dtype: float64

获取pandas系列的头部: pandas.Series.head()

# Get the head of the sample pandas Series
print('First 10 values of the sample pandas Series:\n')
temp_sr = sr.head(10)
print(temp_sr)

输出

First 10 values of the sample pandas Series:

0   -0.157775
1   -0.108095
2   -0.876501
3   -0.591994
4   -0.435755
5   -1.204434
6   -0.035977
7    0.015345
8   -0.453117
9   -0.695302
dtype: float64

获取pandas系列的尾部:pandas.Series.tail()

# Get the tail of the sample pandas Series
print('Last 10 values of the sample pandas Series:\n')
temp_sr = sr.tail(10)
print(temp_sr)

输出

Last 10 values of the sample pandas Series:

990   -0.239336
991   -1.475996
992   -0.162860
993    0.405505
994    0.458872
995    1.186127
996   -0.898278
997   -0.267392
998    1.295608
999   -2.024564
dtype: float64

归纳总结

在这个 Python 教程中,我们已经学习了如何使用head()tail() 函数来获得 pandas DataFrame 或 Series 的头和尾。我们还看到了如何使用Pandas中的pandas.option_context() 函数同时获得一个pandas数据框架的头部和尾部。希望你已经理解了上面讨论的内容,并对使用pandas函数来快速了解你的大型pandas数据框架感到兴奋。谢谢你的阅读!请继续关注我们,了解更多关于Python编程的学习资源。