zoukankan      html  css  js  c++  java
  • 在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中的列

  • 相关阅读:
    VS2005 无法启动调试 (如果你用的是IE8的)
    ClickOnce:部署,强制要求用户使用最新发布版本才可以执行软件的方法
    .Net 中的webBrowser控件加载网页时实现进度显示
    C# :DataGridView中使按下Enter键达到与按下Tab键一样的效果?
    Jquery:getJSON方法解决跨站ajax (json的解剖和运用) 附图片加载时的loading显示...
    GoogleMap : [Google Map]GMark事件运用(GEvent)
    Expression Designer系列工具汇总
    webBrowser:在extendedwebbrowser中实现IDocHostShowUI.ShowMessage 并判断或触发相应事件
    C#: 中文和UNICODE字符转换方法 及仿安居客地图实现。。。错误 GMap2未定义 解决办法 VB 中文转为UNICODE字符
    Jquery :Ajax 自动完成下拉列表。。。。。
  • 原文地址:https://www.cnblogs.com/a00ium/p/13874909.html
Copyright © 2011-2022 走看看