简体   繁体   English

遍历 Python 中的数据框的最佳方法是什么?

[英]What is the best way to iterate through a data frame in Python?

I trying to build a data frame based on another one.我试图建立一个基于另一个数据框。 In order to build the second one, I need to loop over the first data frame and make some changes to the data and insert it in the second one.为了构建第二个,我需要遍历第一个数据帧并对数据进行一些更改并将其插入第二个。 I am using a namedTuple for my for loop.我正在为我的 for 循环使用 namedTuple。

This loop is taking a lot of time to process 2m rows of data.这个循环需要大量时间来处理 2m 行数据。 Is there any fastest way to do this?有没有最快的方法来做到这一点?

Since usually pandas dataframe were built on columns, it seems that it cannot provide a way to iterate through lines.由于通常 pandas dataframe 是建立在列上的,因此它似乎无法提供一种遍历行的方法。 However, This is the way I use for processing each row from the pandas dataframe:但是,这是我用于处理 pandas dataframe 中的每一行的方式:

rows = zip(*(table.loc[:, each] for each in table))
for rowNum, record in enumerate(rows):
    # If you want to process record, modify the code to process here:
    # Otherwise can just print each row
    print("Row", rowNum, "records: ", record)

Btw, I still suggest you to look for some pandas methods that can help you process your first dataframe - usually will be quicker and more effective than you write your own.顺便说一句,我仍然建议您寻找一些 pandas 方法来帮助您处理您的第一个 dataframe - 通常会比您自己编写的更快、更有效。 Wish this could help.希望这能有所帮助。

I'd recommend using the iterrows function that is built into pandas.我建议使用内置于 pandas 中的iterrows function。

data = {'Name': ['John', 'Paul', 'George'], 'Age': [20, 21, 19]}
  db = pd.DataFrame(data)
  print(f"Dataframe:\n{db}\n")
    for row, col in db.iterrows():
      print(f"Row Index:{row}")
      print(f"Column:\n{col}\n")

The output of the above:以上的output:

Dataframe:
     Name  Age
0    John   20
1    Paul   21
2  George   19

Row Index:0
Column:
Name    John
Age       20
Name: 0, dtype: object

Row Index:1
Column:
Name    Paul
Age       21
Name: 1, dtype: object

Row Index:2
Column:
Name    George
Age         19
Name: 2, dtype: object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM