【问题标题】:How to iterate over items one by one on a dataframe?如何在数据框中逐个迭代项目?
【发布时间】:2018-01-20 22:32:17
【问题描述】:

我有以下数据框:

                                         Date   Price
Equity(231 [IBM])   2016-05-10 00:00:00+00:00  150.04
Equity(2574 [TSLA]) 2016-04-29 00:00:00+00:00  248.43

我尝试使用以下方法迭代数据框:

for row in df.itertuples():
    print("symbol :\n",row[0])
    print("Date :\n",row[1])
    print("Price :\n",row[2])

输出:

symbol : Equity(231 [IBM])
Date : 2016-05-10 00:00:00+00:00
Price : 150.04
symbol : Equity(2574 [TSLA])
Date : 2016-04-29 00:00:00+00:00
Price : 248.43

我不想要上面的输出,因为它迭代了 IBM 和 TSLA 而不是单独的 IBM。 当我们迭代时,我想要以下输出:

Out:

symbol : Equity(231 [IBM])
Date : 2016-05-10 00:00:00+00:00
Price : 150.04

在下一次迭代中:

Out:

symbol : Equity(2574 [TSLA])
Date : 2016-04-29 00:00:00+00:00
Price : 248.43

如果您仍然觉得难以理解,请随时要求更多说明。

【问题讨论】:

  • 您使用了一个 for 循环,它将遍历所有 df.rows(可用作元组)直到最后一行然后结束(我的意思是退出循环)。首先检查您是否真的需要 for 循环或任何类型的对数据帧的条件访问。

标签: python loops pandas numpy dataframe


【解决方案1】:

您可以使用DataFrame.itertuplesDataFrame.iterrows

for row in df.itertuples():
    print("symbol :",row.Index)
    print("Date :",row.Date)
    print("Price :",row.Price)
    print ('***************************'

symbol : Equity(231 [IBM]))
Date : 2016-05-10 00:00:00+00:00
Price : 150.04
***************************
symbol : Equity(2574 [TSLA])
Date : 2016-04-29 00:00:00+00:00
Price : 248.43
***************************

for idx, row in df.iterrows():
    print("symbol :",idx)
    print("Date :",row.Date)
    print("Price :",row.Price)
    print ('***************************')

symbol : Equity(231 [IBM])
Date : 2016-05-10 00:00:00+00:00
Price : 150.04
***************************
symbol : Equity(2574 [TSLA])
Date : 2016-04-29 00:00:00+00:00
Price : 248.43
***************************

【讨论】:

  • 我只想要IBMs 在第一次迭代时的属性和TSLA 在第二次迭代:)
  • 这应该是你在你编写的代码中得到的。
  • @ArJuN Dude,如果您指定要执行的操作,那将非常有帮助。或者你可以通过创建一个函数来使用.apply。如果您在 pandas 中,则使用 for 循环不是一个好主意。祝你好运
  • 它不会同时返回两行。它一个接一个地返回。在最后一个 print 语句之后调用该函数。当您打印数据时,它看起来是两行,实际上它们是一个接一个地返回的。你有很多关于循环的知识。
  • @jezrael 对不起,打扰你了,实际上,我以前没有在元组上使用过 for 循环,所以这让我有点困惑,我现在感到羞辱:(
【解决方案2】:

还不确定,您到底想要什么,但请在下面尝试..

for idx in range(len(df)):
    desired_row = df.ix[idx]  #gets first row for 1st iteration as a series
    print(desired_row) ###you can call any function as per requirement here.

如果您想遍历数据框,我看不出您的试用或@Jezrael 的回答有什么问题。

【讨论】:

  • 感谢您的回答,但它引发了TypeError: 'DataFrame' object cannot be interpreted as an integer
  • 好,所有发布的答案都是正确的,但我的错误是不明白for loop 如何处理元组,因此造成了这种混乱,无论如何,谢谢。
【解决方案3】:

如果您尝试iterrows 会怎样?

for index, row in df.iterrows():
    print("symbol :\n",row[0])
    print("Date :\n",row[1])
    print("Price :\n",row[2])

【讨论】:

  • IndexError: index out of bounds
猜你喜欢
  • 1970-01-01
  • 2019-07-05
  • 2014-06-13
  • 2021-12-09
  • 2019-11-17
  • 1970-01-01
  • 2018-06-12
  • 2010-12-06
相关资源
最近更新 更多