• Pandas Dataframe中迭代行的几种方法


    在本文中,我们将介绍如何在Pandas中迭代DataFrame中的行。

    Python是进行数据分析的一种很好的语言,主要是因为以数据为中心的Python包的奇妙生态系统。Pandas就是其中之一,它使导入和分析数据变得更加容易。

    1. 使用Dataframe的index属性

    # import pandas package as pd
    import pandas as pd
      
    # Define a dictionary containing students data
    data = {'Name': ['Ankit', 'Amit',
                     'Aishwarya', 'Priyanka'],
            'Age': [21, 19, 20, 18],
            'Stream': ['Math', 'Commerce',
                       'Arts', 'Biology'],
            'Percentage': [88, 92, 95, 70]}
      
    # Convert the dictionary into DataFrame
    df = pd.DataFrame(data, columns=['Name', 'Age', 
                                     'Stream', 'Percentage'])
      
    print("Given Dataframe :\n", df)
      
    print("\nIterating over rows using index attribute :\n")
      
    # iterate through each row and select
    # 'Name' and 'Stream' column respectively.
    for ind in df.index:
        print(df['Name'][ind], df['Stream'][ind])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    输出

    Given Dataframe :
             Name  Age    Stream  Percentage
    0      Ankit   21      Math          88
    1       Amit   19  Commerce          92
    2  Aishwarya   20      Arts          95
    3   Priyanka   18   Biology          70
    
    Iterating over rows using index attribute :
    
    Ankit Math
    Amit Commerce
    Aishwarya Arts
    Priyanka Biology
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2. 使用DataFrame的loc

    # import pandas package as pd
    import pandas as pd
      
    # Define a dictionary containing students data
    data = {'Name': ['Ankit', 'Amit',
                     'Aishwarya', 'Priyanka'],
            'Age': [21, 19, 20, 18],
            'Stream': ['Math', 'Commerce',
                       'Arts', 'Biology'],
            'Percentage': [88, 92, 95, 70]}
      
    # Convert the dictionary into DataFrame
    df = pd.DataFrame(data, columns=['Name', 'Age',
                                     'Stream', 
                                     'Percentage'])
      
    print("Given Dataframe :\n", df)
      
    print("\nIterating over rows using loc function :\n")
      
    # iterate through each row and select
    # 'Name' and 'Age' column respectively.
    for i in range(len(df)):
        print(df.loc[i, "Name"], df.loc[i, "Age"])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    输出

    Given Dataframe :
             Name  Age    Stream  Percentage
    0      Ankit   21      Math          88
    1       Amit   19  Commerce          92
    2  Aishwarya   20      Arts          95
    3   Priyanka   18   Biology          70
    
    Iterating over rows using loc function :
    
    Ankit 21
    Amit 19
    Aishwarya 20
    Priyanka 18
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3. 使用DataFrame的iloc

    # import pandas package as pd
    import pandas as pd
      
    # Define a dictionary containing students data
    data = {'Name': ['Ankit', 'Amit', 
                     'Aishwarya', 'Priyanka'],
            'Age': [21, 19, 20, 18],
            'Stream': ['Math', 'Commerce', 
                       'Arts', 'Biology'],
            'Percentage': [88, 92, 95, 70]}
      
    # Convert the dictionary into DataFrame
    df = pd.DataFrame(data, columns=['Name', 'Age',
                                     'Stream', 'Percentage'])
      
    print("Given Dataframe :\n", df)
      
    print("\nIterating over rows using iloc function :\n")
      
    # iterate through each row and select
    # 0th and 2nd index column respectively.
    for i in range(len(df)):
        print(df.iloc[i, 0], df.iloc[i, 2])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    输出

    Given Dataframe :
             Name  Age    Stream  Percentage
    0      Ankit   21      Math          88
    1       Amit   19  Commerce          92
    2  Aishwarya   20      Arts          95
    3   Priyanka   18   Biology          70
    
    Iterating over rows using iloc function :
    
    Ankit Math
    Amit Commerce
    Aishwarya Arts
    Priyanka Biology
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4. 使用Dataframe的iterrows()

    # import pandas package as pd
    import pandas as pd
      
    # Define a dictionary containing students data
    data = {'Name': ['Ankit', 'Amit', 
                     'Aishwarya', 'Priyanka'],
            'Age': [21, 19, 20, 18],
            'Stream': ['Math', 'Commerce',
                       'Arts', 'Biology'],
            'Percentage': [88, 92, 95, 70]}
      
    # Convert the dictionary into DataFrame
    df = pd.DataFrame(data, columns=['Name', 'Age', 
                                     'Stream', 'Percentage'])
      
    print("Given Dataframe :\n", df)
      
    print("\nIterating over rows using iterrows() method :\n")
      
    # iterate through each row and select
    # 'Name' and 'Age' column respectively.
    for index, row in df.iterrows():
        print(row["Name"], row["Age"])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    输出

    Given Dataframe :
             Name  Age    Stream  Percentage
    0      Ankit   21      Math          88
    1       Amit   19  Commerce          92
    2  Aishwarya   20      Arts          95
    3   Priyanka   18   Biology          70
    
    Iterating over rows using iterrows() method :
    
    Ankit 21
    Amit 19
    Aishwarya 20
    Priyanka 18
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    5. 使用Dataframe的itertuples()

    # import pandas package as pd
    import pandas as pd
      
    # Define a dictionary containing students data
    data = {'Name': ['Ankit', 'Amit', 'Aishwarya',
                     'Priyanka'],
            'Age': [21, 19, 20, 18],
            'Stream': ['Math', 'Commerce', 'Arts', 
                       'Biology'],
            'Percentage': [88, 92, 95, 70]}
      
    # Convert the dictionary into DataFrame
    df = pd.DataFrame(data, columns=['Name', 'Age', 
                                     'Stream',
                                     'Percentage'])
      
    print("Given Dataframe :\n", df)
      
    print("\nIterating over rows using itertuples() method :\n")
      
    # iterate through each row and select
    # 'Name' and 'Percentage' column respectively.
    for row in df.itertuples(index=True, name='Pandas'):
        print(getattr(row, "Name"), getattr(row, "Percentage"))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    输出

    Given Dataframe :
             Name  Age    Stream  Percentage
    0      Ankit   21      Math          88
    1       Amit   19  Commerce          92
    2  Aishwarya   20      Arts          95
    3   Priyanka   18   Biology          70
    
    Iterating over rows using itertuples() method :
    
    Ankit 88
    Amit 92
    Aishwarya 95
    Priyanka 70
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    6. 使用DataFrame的apply()

    # import pandas package as pd
    import pandas as pd
    
    # Define a dictionary containing students data
    data = {'Name': ['Ankit', 'Amit', 'Aishwarya',
    				'Priyanka'],
    		'Age': [21, 19, 20, 18],
    		'Stream': ['Math', 'Commerce', 'Arts',
    				'Biology'],
    		'Percentage': [88, 92, 95, 70]}
    
    # Convert the dictionary into DataFrame
    df = pd.DataFrame(data, columns=['Name', 'Age', 'Stream',
    								'Percentage'])
    
    print("Given Dataframe :\n", df)
    
    print("\nIterating over rows using apply function :\n")
    
    # iterate through each row and concatenate
    # 'Name' and 'Percentage' column respectively.
    print(df.apply(lambda row: row["Name"] + " " +
    			str(row["Percentage"]), axis=1))
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    输出

    Given Dataframe :
             Name  Age    Stream  Percentage
    0      Ankit   21      Math          88
    1       Amit   19  Commerce          92
    2  Aishwarya   20      Arts          95
    3   Priyanka   18   Biology          70
    
    Iterating over rows using apply function :
    
    0        Ankit 88
    1         Amit 92
    2    Aishwarya 95
    3     Priyanka 70
    dtype: object
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    KingbaseES客户端编程接口指南-ODBC(4. 创建数据源)
    前端框架引入excel表格
    cesium文字实现避让功能
    Python提示<_io.TextIOWrapper name=‘静夜思‘ mode=‘r‘ encoding=‘utf-8‘>解决方法 Python
    链路层3:VLAN的配置与分析
    webpack
    URLDNS链
    设计模式—结构型模式之适配器模式
    【老生谈算法】matlab编写PSO算法及实例——PSO算法
    3.MySQL数据类型详解
  • 原文地址:https://blog.csdn.net/qq_42034590/article/details/132150179