Pandas Series.head() 函式

Minahil Noor 2023年1月30日
  1. pandas.Series.head() 的語法
  2. 示例程式碼:Series.head() 方法返回 Series 的前 5 個值
  3. 示例程式碼:Series.head() 方法返回 Series 的前 7 個值
  4. 示例程式碼:Series.head() 返回空 Series 的方法
Pandas Series.head() 函式

Python Pandas Series.head() 函式從開頭返回選定的值。

pandas.Series.head() 的語法

Series.head(n=5)

引數

n 它是一個整數引數。它指定了要選擇和返回的值的數量。

返回值

它返回 Series 的前 n 個值。

示例程式碼:Series.head() 方法返回 Series 的前 5 個值

import pandas as pd

series = pd.Series([ 'Rose', 'Jasmine', 'Lili', 'Tulip', 'Hibiscus', 'Sun Flower', 'Orchid', 'Carnation','Irises', 'Gardenias'])
print("The Original Series is: \n")
print(series)

series1 = series.head()
print("The First Five Values of Series From Start are: \n")
print(series1)

輸出:

The Original Series is: 

0          Rose
1       Jasmine
2          Lili
3         Tulip
4      Hibiscus
5    Sun Flower
6        Orchid
7     Carnation
8        Irises
9     Gardenias
dtype: object
The First Five Values of Series From Start are: 

0        Rose
1     Jasmine
2        Lili
3       Tulip
4    Hibiscus
dtype: object

我們沒有傳入任何引數,因為 n 的預設值是 5。函式已經返回了給定 Series 的前五個值。

示例程式碼:Series.head() 方法返回 Series 的前 7 個值

import pandas as pd

series = pd.Series([ 'Rose', 'Jasmine', 'Lili', 'Tulip', 'Hibiscus', 'Sun Flower', 'Orchid', 'Carnation','Irises', 'Gardenias'])
print("The Original Series is: \n")
print(series)

series1 = series.head(7)
print("The First Seven Values of Series From Start are: \n")
print(series1)

輸出:

The Original Series is: 

0          Rose
1       Jasmine
2          Lili
3         Tulip
4      Hibiscus
5    Sun Flower
6        Orchid
7     Carnation
8        Irises
9     Gardenias
dtype: object
The First Seven Values of Series From Start are: 

0          Rose
1       Jasmine
2          Lili
3         Tulip
4      Hibiscus
5    Sun Flower
6        Orchid
dtype: object

我們已經通過 n=7。該函式返回了給定 Series 的前七個值。

示例程式碼:Series.head() 返回空 Series 的方法

import pandas as pd

series = pd.Series([ 'Rose', 'Jasmine', 'Lili', 'Tulip', 'Hibiscus', 'Sun Flower', 'Orchid', 'Carnation','Irises', 'Gardenias'])
print("The Original Series is: \n")
print(series)

series1 = series.head(0)
print("The Empty Series is: \n")
print(series1)

輸出:

The Original Series is: 

0          Rose
1       Jasmine
2          Lili
3         Tulip
4      Hibiscus
5    Sun Flower
6        Orchid
7     Carnation
8        Irises
9     Gardenias
dtype: object
The Empty Series is: 

Series([], dtype: object)

函式返回一個空序列。

相關文章 - Pandas Series