简体   繁体   中英

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. The index is a date/timestamp.

If the two columns are signal and value , I try:

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. So you should do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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