[英]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:如果这两列是
signal
和value
,我尝试:
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.