简体   繁体   English

如何在熊猫数据框中逐行迭代并在其列中查找值

[英]How to iterate row by row in a pandas dataframe and look for a value in its columns

I must read each row of an excel file and preform calculations based on the contents of each row. 我必须阅读excel文件的每一行,并根据每一行的内容执行计算。 Each row is divided in columns, my problem is that I cannot find a way to access the contents of those columns. 每行都分为几列,我的问题是我无法找到一种方法来访问这些列的内容。

I'm reading the rows with: 我正在阅读以下行:

for i in df.index,:
    print(df.loc[i])

Which works well, but when I try to access, say, the 4h column with this type of indexing I get an error: 效果很好,但是当我尝试使用这种索引类型访问4h列时,出现错误:

for i in df.index,:
    print(df.loc[i][3])

I'm pretty sure I'm approaching the indexing issue in the wrong way, but I cannot figure put how to solve it. 我很确定我以错误的方式解决了索引问题,但是我无法弄清楚如何解决它。

You can use iterrows(), like in the following code: 您可以使用iterrows(),如以下代码所示:

for index, row in dataFrame.iterrows():
  print(row)

But this is not the most efficient way to iterate over a panda DataFrame, more info at this post. 但这不是迭代熊猫DataFrame的最有效方法,有关更多信息,请参见本文。

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

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