简体   繁体   English

如何使用时间戳索引遍历 pandas dataframe?

[英]How to iterate through a pandas dataframe with a timestamp index?

I'm trying to iterate through a dataframe using iterrows and pull out two columns values.我正在尝试使用 iterrows 遍历 dataframe 并提取两列值。 The index is a date/timestamp.索引是日期/时间戳。

If the two columns are signal and value , I try:如果这两列是signalvalue ,我尝试:

for row in df_train.iterrows():
    for value in row:
        print(value['signal'])

I get the following traceback:我得到以下回溯:

Traceback (most recent call last):

  File "<ipython-input-148-34d711ac79ad>", line 3, in <module>
    print(value['RSI_20'])

TypeError: 'Timestamp' object is not subscriptable

The iterrows method actually yields two values: the index and the values of the row. iterrows方法实际上产生两个值:索引和行的值。 So you should do:所以你应该这样做:

for ts, row in df_train.iterrows():
    print(ts, row['signal'], row['value'])

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

相关问题 通过Pandas Dataframe进行迭代和索引的最快方法 - Fastest way to iterate and index through Pandas Dataframe 通过索引迭代数据帧 - Iterate through a dataframe by index 如何遍历 pandas dataframe 中的嵌套 for 循环? - How to iterate through a nested for loop in pandas dataframe? 遍历 Pandas Dataframe 中定义的日期时间索引范围 - Iterate through defined Datetime index range in Pandas Dataframe Python熊猫遍历数据框 - Python pandas iterate through dataframe 如何使用索引迭代熊猫多索引数据帧 - How to iterate over pandas multiindex dataframe using index 如何迭代和计算 pandas 多索引 dataframe - How to iterate and calculate over a pandas multi-index dataframe 如何在 Pandas 中的 DataFrame 中迭代行和索引以过滤布尔值 - How to iterate over rows and index in a DataFrame in Pandas to filter bolean values 创建生成器时如何避免将索引更改为熊猫数据框中的时间戳 - how avoid change index to timestamp in pandas dataframe when create a generator Python / Pandas / DataFrame /迭代/遍历行,找到一个值,记下索引,然后从该索引开始,找到另一个值 - Python/ Pandas/ DataFrame/ Iteration/ Iterate through rows, find a value, note index, then starting at that index, find another value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM