如何解决迭代中的 KeyError?

huangapple go评论126阅读模式
英文:

How to solve KeyError for iteration?

问题

我目前有一个包含名为timeAccepted的列的数据框架。

我正在尝试在每次迭代时打印timeAccepted的值。我只是使用以下代码:

  1. def print_time(df):
  2. print(df['timeAccepted'])
  3. for i, row in df.iterrows():
  4. print(df.columns)
  5. print(df.at[i, 'timeAccepted'])

但是我得到以下错误:

  1. KeyError: 'timeAccepted'

请注意,print(df['timeAccepted'])的输出是:

  1. 0 2023-07-27T06:50:03.747135Z
  2. 1 2023-07-27T06:50:06.030559Z
  3. 2 2023-07-27T06:50:08.025268Z
  4. 3 2023-07-27T06:50:10.024531Z
  5. 4 2023-07-27T06:50:12.028957Z
  6. ...
  7. 26232 2023-07-27T12:46:27.024663Z
  8. 26233 2023-07-27T12:46:27.024663Z
  9. 26234 2023-07-27T12:45:02.027558Z
  10. 26235 2023-07-27T12:46:29.023594Z
  11. 26236 2023-07-27T12:46:29.023594Z
  12. Name: timeAccepted, Length: 26237, dtype: object

print(df.columns)

  1. Index(['Order identification code', 'Initial quantity', 'side', 'Order type',
  2. 'timeInForce', 'Limit price', 'quoteId', 'userId', 'timeAccepted'],
  3. dtype='object')

所以,我已经检查了数据框中是否存在该列,但仍然出现KeyError!请帮助!

英文:

I currently have a dataframe which include a column called timeAccepted.

I'm trying to print the value of timeAccepted for every iteration. I'm simply using the following code:

  1. def print_time(df):
  2. print(df['timeAccepted'])
  3. for i, row in df.iterrows():
  4. print(df.columns)
  5. print(df.at[i, 'timeAccepted'])

But I'm getting the following error:

  1. KeyError: 'timeAccepted'

please note that print(df['timeAccepted']) have as an output:

  1. 0 2023-07-27T06:50:03.747135Z
  2. 1 2023-07-27T06:50:06.030559Z
  3. 2 2023-07-27T06:50:08.025268Z
  4. 3 2023-07-27T06:50:10.024531Z
  5. 4 2023-07-27T06:50:12.028957Z
  6. ...
  7. 26232 2023-07-27T12:46:27.024663Z
  8. 26233 2023-07-27T12:46:27.024663Z
  9. 26234 2023-07-27T12:45:02.027558Z
  10. 26235 2023-07-27T12:46:29.023594Z
  11. 26236 2023-07-27T12:46:29.023594Z
  12. Name: timeAccepted, Length: 26237, dtype: object

and print(df.columns):

  1. Index(['Order identification code', 'Initial quantity', 'side', 'Order type',
  2. 'timeInForce', 'Limit price', 'quoteId', 'userId', 'timeAccepted'], dtype='object')

So, I've checked and the column do exist in the dataframe but I'm still having the KeyError! Please help!

答案1

得分: 1

尝试更深入地检查索引名称:

  1. index_names = df.columns.to_list()
  2. time_accepted_index = index_names[-1]
  3. print(len(time_accepted_index))
  4. if not time_accepted_index == 'timeAccepted':
  5. print('strings are not the same')

我遇到了一个类似的情况,索引名称末尾有空格。

英文:

Try to inspect the Index names a bit deeper:

  1. index_names = df.columns.to_list()
  2. time_accepted_index = index_names[-1]
  3. print(len(time_accepted_index))
  4. if not time_accepted_index == 'timeAccepted':
  5. print('strings are not the same')

I got a similar case where the index name had trailing whitespaces.

huangapple
  • 本文由 发表于 2023年7月27日 21:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定