a00ium
 

在本文中,我们将讨论如何循环或迭代DataFrame的全部或某些列?有多种方法可以完成此任务。

首先创建一个数据框,然后看一下:
代码:

# import pandas package 
import pandas as pd 

# List of Tuples 
students = [(\'Ankit\', 22, \'A\'), 
        (\'Swapnil\', 22, \'B\'), 
        (\'Priya\', 22, \'B\'), 
        (\'Shivangi\', 22, \'B\'), 
            ] 
# Create a DataFrame object 
stu_df = pd.DataFrame(students, columns =[\'Name\', \'Age\', \'Section\'], 
                    index =[\'1\', \'2\', \'3\', \'4\']) 

stu_df 

 

输出:

大熊猫

现在,让我们看看不同的方式来迭代DataFrame或某些列:

方法#1: 使用DataFrame.iteritems()
Dataframe类提供了一个成员函数iteritems(),该函数提供了一个迭代器,该迭代器可用于迭代数据帧的所有列。对于Dataframe中的每一列,它将返回一个迭代器到包含列名称及其内容为序列的元组。

代码:

import pandas as pd 


# List of Tuples 
students = [(\'Ankit\', 22, \'A\'), 
        (\'Swapnil\', 22, \'B\'), 
        (\'Priya\', 22, \'B\'), 
        (\'Shivangi\', 22, \'B\'), 
            ] 

# Create a DataFrame object 
stu_df = pd.DataFrame(students, columns =[\'Name\', \'Age\', \'Section\'], 
                    index =[\'1\', \'2\', \'3\', \'4\']) 

# gives a tuple of column name and series 
# for each column in the dataframe 
for (columnName, columnData) in stu_df.iteritems(): 
    print(\'Colunm Name : \', columnName) 
    print(\'Column Contents : \', columnData.values) 
输出:

遍历dataframe-1中的列

方法2: 使用[]运算符:
我们可以遍历列名并选择所需的列。

代码:

import pandas as pd 


# List of Tuples 
students = [(\'Ankit\', 22, \'A\'), 
        (\'Swapnil\', 22, \'B\'), 
        (\'Priya\', 22, \'B\'), 
        (\'Shivangi\', 22, \'B\'), 
            ] 

# Create a DataFrame object 
stu_df = pd.DataFrame(students, columns =[\'Name\', \'Age\', \'Section\'], 
                    index =[\'1\', \'2\', \'3\', \'4\']) 

# Iterate over column names 
for column in stu_df: 
    
    # Select column contents by column 
    # name using [] operator 
    columnSeriesObj = stu_df[column] 
    print(\'Colunm Name : \', column) 
    print(\'Column Contents : \', columnSeriesObj.values) 

输出:

遍历dataframe-2中的列

方法3: 迭代多于一列:
假设我们需要迭代多于一列。为此,我们可以从数据框中选择多个列并对其进行迭代。

代码:

import pandas as pd 


# List of Tuples 
students = [(\'Ankit\', 22, \'A\'), 
        (\'Swapnil\', 22, \'B\'), 
        (\'Priya\', 22, \'B\'), 
        (\'Shivangi\', 22, \'B\'), 
            ] 

# Create a DataFrame object 
stu_df = pd.DataFrame(students, columns =[\'Name\', \'Age\', \'Section\'], 
                    index =[\'1\', \'2\', \'3\', \'4\']) 

# Iterate over two given columns 
# only from the dataframe 
for column in stu_df[[\'Name\', \'Section\']]: 
    
    # Select column contents by column 
    # name using [] operator 
    columnSeriesObj = stu_df[column] 
    print(\'Colunm Name : \', column) 
    print(\'Column Contents : \', columnSeriesObj.values) 

 

输出:

遍历dataframe-3中的列

方法4: 以相反的顺序迭代列:
我们也可以以相反的顺序遍历列。

代码:

import pandas as pd 


# List of Tuples 
students = [(\'Ankit\', 22, \'A\'), 
        (\'Swapnil\', 22, \'B\'), 
        (\'Priya\', 22, \'B\'), 
        (\'Shivangi\', 22, \'B\'), 
            ] 

# Create a DataFrame object 
stu_df = pd.DataFrame(students, columns =[\'Name\', \'Age\', \'Section\'], 
                    index =[\'1\', \'2\', \'3\', \'4\']) 


# Iterate over the sequence of column names 
# in reverse order 
for column in reversed(stu_df.columns): 
    
    # Select column contents by column 
    # name using [] operator 
    columnSeriesObj = stu_df[column] 
    print(\'Colunm Name : \', column) 
    print(\'Column Contents : \', columnSeriesObj.values) 

输出:

遍历dataframe-4中的列

方法5: 使用索引(iloc):
要按索引遍历Dataframe的列,我们可以遍历一个范围(即0到最大列数),而对于每个索引,我们可以使用iloc []选择列的内容。

代码:

import pandas as pd 


# List of Tuples 
students = [(\'Ankit\', 22, \'A\'), 
        (\'Swapnil\', 22, \'B\'), 
        (\'Priya\', 22, \'B\'), 
        (\'Shivangi\', 22, \'B\'), 
            ] 

# Create a DataFrame object 
stu_df = pd.DataFrame(students, columns =[\'Name\', \'Age\', \'Section\'], 
                    index =[\'1\', \'2\', \'3\', \'4\']) 


# Iterate over the index range from 
# 0 to max number of columns in dataframe 
for index in range(stu_df.shape[1]): 
    
    print(\'Column Number : \', index) 
    
    # Select column by index position using iloc[] 
    columnSeriesObj = stu_df.iloc[:, index] 
    print(\'Column Contents : \', columnSeriesObj.values) 

 

输出:

遍历dataframe-5中的列

 

分类:

技术点:

相关文章:

  • 2021-08-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
  • 2021-12-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-16
  • 2022-12-23
  • 2022-12-23
  • 2021-10-29
  • 2022-02-06
相关资源
相似解决方案