Pandas DataFrame迭代器

  • iterrows(): 将DataFrame迭代为(insex, Series)对。
  • itertuples(): 将DataFrame迭代为元祖。
  • iteritems(): 将DataFrame迭代为(列名, Series)对
import pandas as pd
 
inp = [{'c1':10, 'c2':100}, {'c1':11, 'c2':110}, {'c1':12, 'c2':123}]
df = pd.DataFrame(inp)

# iterrows
for date, row in df.iterrows():
    print(row['c1'], row['c2'])

# iteritems
for date, row in df.iteritems():
    print(row[0], row[1], row[2])

# itertuples
for row in df.itertuples():
    print(getattr(row, 'c1'), getattr(row, 'c2'))

 

你可能感兴趣的:(Python,python)