在Pandas Dataframe中遍历行的不同方法

Posted a00ium

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Pandas Dataframe中遍历行的不同方法相关的知识,希望对你有一定的参考价值。

 

Python是进行数据分析的一种出色语言,主要是因为以数据为中心的Python软件包具有奇妙的生态系统。Pandas是其中的一种,使导入和分析数据更加容易。

让我们看看在Pandas Dataframe中遍历行的不同方法

方法#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 :
", df) 

print("
Iterating over rows using index attribute :
") 

# iterate through each row and select 
# Name and Stream column respectively. 
for ind in df.index: 
    print(df[Name][ind], df[Stream][ind]) 
输出: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

 

方法2:使用数据框的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 :
", df) 

print("
Iterating over rows using loc function :
") 

# 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"]) 

输出:

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

 


方法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 :
", df) 

print("
Iterating over rows using iloc function :
") 

# 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]) 
输出:
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

 

方法4:使用数据框的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 :
", df) 

print("
Iterating over rows using iterrows() method :
") 

# iterate through each row and select 
# Name and Age column respectively. 
for index, row in df.iterrows(): 
    print (row["Name"], row["Age"]) 

输出:

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

 


方法5:使用数据框的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 :
", df) 

print("
Iterating over rows using itertuples() method :
") 

# 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")) 

输出:

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

方法6:使用数据框的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 :
", df) 

print("
Iterating over rows using apply function :
") 

# iterate through each row and concatenate 
# Name and Percentage column respectively. 
print(df.apply(lambda row: row["Name"] + " " + str(row["Percentage"]), axis = 1)) 

输出:

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

 

 



以上是关于在Pandas Dataframe中遍历行的不同方法的主要内容,如果未能解决你的问题,请参考以下文章

循环遍历 Pandas DataFrame 会产生 ValueError

11.pandas里面的一些常用方法

为啥pandas.DataFrame.sum(axis=0) 在axis = 0代表行的每一列中返回值的总和?

如何在 Pandas 中遍历 DataFrame 中的行

如何在 Pandas 中遍历 DataFrame 中的行

pandas使用duplicated函数统计dataframe中重复数据行的个数(counting the number of duplicate rows in dataframe)