在Pandas Python中从数据框架中选择任何行

在Pandas Python中从数据框架中选择任何行

在这篇文章中,我们将学习如何在不使用ilic[]等函数的情况下,以列表的形式从一个数据框架中获取记录。有多种方法可以从给定的数据框架中以列表的形式获取行。让我们在例子的帮助下看看这些方法。

# 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]}) 
  
  
# using interrors() method
  
# Create an empty list 
Row_list =[] 
    
# Iterate over each row 
for index, rows in df.iterrows(): 
  
    # Create list for the current row 
    my_list =[rows.Date, rows.Event, rows.Cost] 
        
    # append the list to the final list 
    Row_list.append(my_list) 
    
# Print the list 
print(Row_list) 
Python

输出:

[['10/2/2011', 'Music', 10000], ['11/2/2011', 'Poetry', 5000], 
      ['12/2/2011', 'Theatre', 15000], ['13/2/11', 'Comedy', 2000]]
Python
    
# Print the first 2 elements 
print(Row_list[:2]) 
Python

输出:

[['10/2/2011', 'Music', 10000], ['11/2/2011', 'Poetry', 5000]]
Python

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册