从列表中迭代 pandas 数据框
Iterating over pandas dataframe from a list
我有一个时间戳列表:
[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')]
我怎样才能只在 pandas 数据帧上迭代具有相应 Date
和 Low
列的时间戳:
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'
我该怎么做才能完成这个?
第 select 行列表和列 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)
我有一个时间戳列表:
[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')]
我怎样才能只在 pandas 数据帧上迭代具有相应 Date
和 Low
列的时间戳:
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'
我该怎么做才能完成这个?
第 select 行列表和列 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)