在Python-Pandas中对数据框架的所有或某些列进行循环或迭代
在这篇文章中,我们将讨论如何循环或迭代一个数据框架的整体或某些列?有各种方法来实现这一任务。
让我们首先创建一个Dataframe,看看.NET技术的应用。
代码 :
# 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
输出 :
现在让我们来看看迭代的不同方式或数据框架的某些列。
方法#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('Column Name : ', columnName)
print('Column Contents : ', columnData.values)
输出:
方法#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('Column Name : ', column)
print('Column Contents : ', columnSeriesObj.values)
输出:
方法三: 迭代一个以上的列:
假设我们需要迭代一个以上的列。为了做到这一点,我们可以从数据框架中选择一个以上的列并对它们进行迭代。
代码 :
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('Column Name : ', column)
print('Column Contents : ', columnSeriesObj.values)
输出:
方法四:以相反顺序迭代列:
我们也可以按相反的顺序对列进行迭代。
代码 :
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('Column Name : ', column)
print('Column Contents : ', columnSeriesObj.values)
输出:
方法#5: 使用索引( iloc ):
为了按索引遍历数据框架的列,我们可以遍历一个范围,即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)
输出: