📜  在 Pandas 中使用 iloc[] 和 iat[] 从 Dataframe 中选择任何行

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

在 Pandas 中使用 iloc[] 和 iat[] 从 Dataframe 中选择任何行

在本文中,我们将学习如何使用 ilic[] 和 iat[] 函数从数据框中获取行作为列表。有多种方法可以从给定的数据框中获取行作为列表。让我们看看他们将在示例的帮助下。

Python
import pandas as pd
   
# Create the dataframe
df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/11'],
                    'Event':['Music', 'Poetry', 'Theatre', 'Comedy'],
                    'Cost':[10000, 5000, 15000, 2000]})
 
# Create an empty list
Row_list =[]
   
# Iterate over each row
for i in range((df.shape[0])):
   
    # Using iloc to access the values of 
    # the current row denoted by "i"
    Row_list.append(list(df.iloc[i, :]))
   
# Print the first 3 elements
print(Row_list[:3])


Python3
# importing pandas as pd
import pandas as pd
   
# Create the dataframe
df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/11'],
                    'Event':['Music', 'Poetry', 'Theatre', 'Comedy'],
                    'Cost':[10000, 5000, 15000, 2000]})
   
# Create an empty list
Row_list =[]
   
# Iterate over each row
for i in range((df.shape[0])):
    # Create a list to store the data
    # of the current row
    cur_row =[]
       
    # iterate over all the columns
    for j in range(df.shape[1]):
           
        # append the data of each
        # column to the list
        cur_row.append(df.iat[i, j])
           
    # append the current row to the list
    Row_list.append(cur_row)
 
# Print the first 3 elements
print(Row_list[:3])


输出:

[[10000, '10/2/2011', 'Music'], [5000, '11/2/2011', 'Poetry'],
      [15000, '12/2/2011', 'Theatre']


使用 iat[] 方法 –

Python3

# importing pandas as pd
import pandas as pd
   
# Create the dataframe
df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/11'],
                    'Event':['Music', 'Poetry', 'Theatre', 'Comedy'],
                    'Cost':[10000, 5000, 15000, 2000]})
   
# Create an empty list
Row_list =[]
   
# Iterate over each row
for i in range((df.shape[0])):
    # Create a list to store the data
    # of the current row
    cur_row =[]
       
    # iterate over all the columns
    for j in range(df.shape[1]):
           
        # append the data of each
        # column to the list
        cur_row.append(df.iat[i, j])
           
    # append the current row to the list
    Row_list.append(cur_row)
 
# Print the first 3 elements
print(Row_list[:3])

输出:

[[10000, '10/2/2011', 'Music'], [5000, '11/2/2011', 'Poetry'], 
      [15000, '12/2/2011', 'Theatre']]