📜  获取给定 DataFrame 的前 3 行

📅  最后修改于: 2022-05-13 01:55:48.961000             🧑  作者: Mango

获取给定 DataFrame 的前 3 行

让我们首先创建一个数据框,然后我们将尝试使用几种方法获取该数据框的前 3 行。

代码:创建数据框。

Python3
# import pandas library
import pandas as pd
  
# dictionary
record = {
  "Name": ["Tom", "Jack", "Lucy",
           "Bob", "Jerry", "Alice",
           "Thomas", "Barbie"],
    
   "Marks": [9, 19, 20, 
             17, 11, 18,
             5, 8], 
    
  "Status": ["Fail", "Pass", "Pass",
             "Pass","Pass", "Pass",
             "Fail", "Fail"]}
  
# converting record into
# pandas dataframe
df = pd.DataFrame(record)
  
# printing whole dataframe
df


Python3
# import pandas library
import pandas as pd
  
# dictionary
record = {
  "Name": ["Tom", "Jack", "Lucy",
           "Bob", "Jerry", "Alice",
           "Thomas", "Barbie"],
    
   "Marks": [9, 19, 20, 
             17, 11, 18,
             5, 8], 
    
  "Status": ["Fail", "Pass", "Pass",
             "Pass","Pass", "Pass",
             "Fail", "Fail"]}
  
# converting record into
# pandas dataframe
df = pd.DataFrame(record)
  
# select first 3 rows
# from the dataframe
df1 = df.head(3)
  
# show the dataframe
df1


Python3
# import pandas library
import pandas as pd
  
# dictionary
record = {
  "Name": ["Tom", "Jack", "Lucy",
           "Bob", "Jerry", "Alice",
           "Thomas", "Barbie"],
    
   "Marks": [9, 19, 20, 
             17, 11, 18,
             5, 8], 
    
  "Status": ["Fail", "Pass", "Pass",
             "Pass","Pass", "Pass",
             "Fail", "Fail"]}
  
# converting record into
# pandas dataframe
df = pd.DataFrame(record)
  
# select first 3 rows
# from dataframe
df2 = df.iloc[0:3]
  
# show the dataframe
df2


Python
# import pandas library
import pandas as pd
  
# dictionary
record = {
  "Name": ["Tom", "Jack", "Lucy",
           "Bob", "Jerry", "Alice",
           "Thomas", "Barbie"],
    
   "Marks": [9, 19, 20, 
             17, 11, 18,
             5, 8], 
    
  "Status": ["Fail", "Pass", "Pass",
             "Pass","Pass", "Pass",
             "Fail", "Fail"]}
  
# converting record into
# pandas dataframe
df = pd.DataFrame(record)
  
# select first 3 rows 
# of the dataframe
df3 = df.iloc[[0, 1, 2]]
  
# show the dataframe
df3


输出:

数据框

上述代码的输出:创建的数据框

获取上述 Dataframe 的前 3 行:

方法一:使用head(n)方法。

此方法返回数据帧的前 n 行,其中 n 是整数值,它指定要显示的行数。 n 的默认值为 5,因此,不带参数的 head函数将数据帧的前五行作为输出。因此,要获取数据帧的前三行,我们可以将 n 的值分配为“3”。

下面是使用 head() 方法获取数据帧前三行的代码:

Python3

# import pandas library
import pandas as pd
  
# dictionary
record = {
  "Name": ["Tom", "Jack", "Lucy",
           "Bob", "Jerry", "Alice",
           "Thomas", "Barbie"],
    
   "Marks": [9, 19, 20, 
             17, 11, 18,
             5, 8], 
    
  "Status": ["Fail", "Pass", "Pass",
             "Pass","Pass", "Pass",
             "Fail", "Fail"]}
  
# converting record into
# pandas dataframe
df = pd.DataFrame(record)
  
# select first 3 rows
# from the dataframe
df1 = df.head(3)
  
# show the dataframe
df1

输出:

上述代码的输出:- 使用 head()函数的数据帧的前三行

方法 2:使用iloc[ ]

这可以用于通过使用我们想要的切片数据帧的起始索引和结束索引来切片数据帧。

因此,如果我们想要前三行,即从索引 0 到索引 2,我们可以使用以下代码:

Python3

# import pandas library
import pandas as pd
  
# dictionary
record = {
  "Name": ["Tom", "Jack", "Lucy",
           "Bob", "Jerry", "Alice",
           "Thomas", "Barbie"],
    
   "Marks": [9, 19, 20, 
             17, 11, 18,
             5, 8], 
    
  "Status": ["Fail", "Pass", "Pass",
             "Pass","Pass", "Pass",
             "Fail", "Fail"]}
  
# converting record into
# pandas dataframe
df = pd.DataFrame(record)
  
# select first 3 rows
# from dataframe
df2 = df.iloc[0:3]
  
# show the dataframe
df2
使用 iloc[] 方法的数据帧的前三行

上述代码的输出:- 使用 iloc[] 方法的数据帧的前三行

方法3:使用行的索引

iloc[ ]方法也可以通过在 iloc 方法中直接声明我们想要的行的索引来使用。说要获取具有索引 m 和 n 的行 iloc[ ] 可以用作:

以下是使用此方法获取数据帧前三行的代码:

Python

# import pandas library
import pandas as pd
  
# dictionary
record = {
  "Name": ["Tom", "Jack", "Lucy",
           "Bob", "Jerry", "Alice",
           "Thomas", "Barbie"],
    
   "Marks": [9, 19, 20, 
             17, 11, 18,
             5, 8], 
    
  "Status": ["Fail", "Pass", "Pass",
             "Pass","Pass", "Pass",
             "Fail", "Fail"]}
  
# converting record into
# pandas dataframe
df = pd.DataFrame(record)
  
# select first 3 rows 
# of the dataframe
df3 = df.iloc[[0, 1, 2]]
  
# show the dataframe
df3

输出:

数据帧的前三行使用 iloc 和所需行的索引。

上述代码的输出:- 数据帧的前三行使用 iloc 和所需行的索引。