在Python-Pandas中循环或遍历数据框的所有或某些列

 

在本文中,我们将讨论如何循环或迭代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中的列

 

posted @   DaisyLinux  阅读(27656)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示