Slartibartfast
Slartibartfast

Reputation: 1190

Iterating over pandas dataframe from a list

I have a list of timestamp:

[Timestamp('2018-01-08 00:00:00'),
 Timestamp('2018-01-22 00:00:00'),
 Timestamp('2019-11-18 00:00:00'),
 Timestamp('2019-12-12 00:00:00'),
 Timestamp('2020-01-06 00:00:00'),
 Timestamp('2020-02-10 00:00:00'),
 Timestamp('2020-04-02 00:00:00')]

How can i iterate only the timestamps over pandas dataframe with corresponding Date and Low columns:

                   High          Low  ...    Adj Close    bcc
Date                                  ...                    
2018-01-02  2695.889893  2682.360107  ...  2695.810059  False
2018-01-03  2714.370117  2697.770020  ...  2713.060059  False
2018-01-04  2729.290039  2719.070068  ...  2723.989990  False
2018-01-05  2743.449951  2727.919922  ...  2743.149902  False
2018-01-08  2748.510010  2737.600098  ...  2747.709961   True
                ...          ...  ...          ...    ...
2020-04-09  2818.570068  2762.360107  ...  2789.820068  False
2020-04-13  2782.459961  2721.169922  ...  2761.629883  False
2020-04-14  2851.850098  2805.100098  ...  2846.060059  False
2020-04-15  2801.879883  2761.540039  ...  2783.360107  False
2020-04-16  2806.510010  2764.320068  ...  2799.550049  False

[576 rows x 7 columns]

Something like :

for i in timestmp:
    for Date, row in data.Low.iterrows():
        print(Low)

The code above is wrong and gives an error: AttributeError: 'Series' object has no attribute 'iterrows' What can i do to accomplish this?

Upvotes: 0

Views: 56

Answers (1)

jezrael
jezrael

Reputation: 862771

First select rows by list and by column Low to Series:

s = df.loc[df.index.isin(L), 'Low']
print (s)
Date
2018-01-08    2737.600098
Name: Low, dtype: float64

And then loop by Series:

for k, v in s.items():
    print (k, v)

Upvotes: 1

Related Questions