python dataframe遍历_Python dataFrame 行列遍历

iteritems():按列遍历,将DataFrame的每一列迭代为(列名, Series)对,可以通过row[index]对元素进行访问。

示例数据

1

2

3

4

5

6

import pandas as pd

inp= [{'c1':10,'c2':100}, {'c1':11,'c2':110}, {'c1':12,'c2':123}]

df= pd.DataFrame(inp)

print(df)

import pandas as pd inp = [{'c1':10, 'c2':100}, {'c1':11, 'c2':110}, {'c1':12, 'c2':123}] df = pd.DataFrame(inp) print(df)

format,png

按行遍历iterrows():

1

2

for index, rowin df.iterrows():

print(index)# 输出每行的索引值

for index, row in df.iterrows(): print(index) # 输出每行的索引值

format,png

row[‘name']

1

2

3

# 对于每一行,通过列名name访问对应的元素

for rowin df.iterrows():

print(row['c1'], row['c2'])# 输出每一行

# 对于每一行,通过列名name访问对应的元素 for row in df.iterrows(): print(row['c1'], row['c2']) # 输出每一行

format,png

按行遍历itertuples():

getattr(row, ‘name')

1

2

for rowin df.itertuples():

print(getattr(row,'c1'),getattr(row,'c2'))# 输出每一行

for row in df.itertuples(): print(getattr(row, 'c1'), getattr(row, 'c2')) # 输出每一行

format,png

按列遍历iteritems():

1

2

for index, rowin df.iteritems():

print(index)# 输出列名

for index, row in df.iteritems(): print(index) # 输出列名

format,png

1

2

for rowin df.iteritems():

print(row[0], row[1], row[2])# 输出各列

for row in df.iteritems(): print(row[0], row[1], row[2]) # 输出各列

format,png

原文链接:https://blog.csdn.net/qq_39349673/article/details/107529206

你可能感兴趣的:(python,dataframe遍历)