我有一个时间戳列表:
[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')]
我如何只迭代带有相应的Date
和Low
列的pandas数据帧上的时间戳:
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]
类似的东西:
for i in timestmp:
for Date, row in data.Low.iterrows():
print(Low)
上面的代码是错误的,并给出一个错误:AttributeError: 'Series' object has no attribute 'iterrows'
我该怎么做?
答案 0 :(得分:1)
首先按列表和列Low
至Series
选择行:
s = df.loc[df.index.isin(L), 'Low']
print (s)
Date
2018-01-08 2737.600098
Name: Low, dtype: float64
然后按系列循环:
for k, v in s.items():
print (k, v)