![](/img/trans.png)
[英]Python: what is the best way to iterate through a dataset to pass the values into a dict?
[英]What is the best way to iterate through a data frame in Python?
我试图建立一个基于另一个数据框。 为了构建第二个,我需要遍历第一个数据帧并对数据进行一些更改并将其插入第二个。 我正在为我的 for 循环使用 namedTuple。
这个循环需要大量时间来处理 2m 行数据。 有没有最快的方法来做到这一点?
由于通常 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)
顺便说一句,我仍然建议您寻找一些 pandas 方法来帮助您处理您的第一个 dataframe - 通常会比您自己编写的更快、更有效。 希望这能有所帮助。
我建议使用内置于 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")
以上的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.