如何用另一个数据框的索引迭代标签?

时间:2017-04-19 16:51:10

标签: python dataframe indexing keyerror

对我来说真是个问题!我有df_000001这样的话:

date          open    high   close     low       volume        amount  price_change

2000-01-04 1368.69 1407.52 1406.37 1361.21    903402300    5801550000
2000-01-05 1407.83 1433.78 1409.68 1398.32   1057998400    8653075000
2000-01-06 1406.04 1463.95 1463.94 1400.25   1348051500   10238351000
2000-01-07 1477.15 1522.83 1516.60 1477.15   3451569900   22007478000

它的索引很清楚:

Index(['2000-01-04', '2000-01-05', '2000-01-06', '2000-01-07'])

虽然我得到了另一个df,类似于df_000001

date        open  high  close   low       volume        amount  price_change

2010-01-04 33.62 34.27  33.50 33.42  13469261.00  549593152.00         -0.12
2010-01-05 33.76 34.97  34.69 33.72  18374008.00  760548096.00          0.93
2010-01-06 34.85 35.87  35.11 34.63  21924930.00  932180353.00          0.26
2010-01-07 35.84 36.34  34.42 34.23  21769572.00  925199040.00         -1.42

它的索引是:

DatetimeIndex(['2010-01-04', '2010-01-05', '2010-01-06', '2010-01-07'])

现在我写了一个for循环迭代:

x1 = [df_000001.loc[si]['close'] for si in df.index]

这意味着要提取df_000001列,其日期索引相同df

即使我努力了,它也只是一直运行错误:

KeyError: 'the label [2010-01-04 00:00:00] is not in the [index]'

我该如何解决这个问题?

0 个答案:

没有答案