如何解决迭代中的 KeyError?

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

How to solve KeyError for iteration?

问题

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

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

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

但是我得到以下错误:

KeyError: 'timeAccepted'

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

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

print(df.columns)

Index(['Order identification code', 'Initial quantity', 'side', 'Order type',
       'timeInForce', 'Limit price', 'quoteId', 'userId', 'timeAccepted'],
      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:

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

But I'm getting the following error:

KeyError: 'timeAccepted'

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

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

and print(df.columns):

Index(['Order identification code', 'Initial quantity', 'side', 'Order type',
       '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

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

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

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

英文:

Try to inspect the Index names a bit deeper:

index_names = df.columns.to_list()
time_accepted_index = index_names[-1]
print(len(time_accepted_index))
if not time_accepted_index == 'timeAccepted':
    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:

确定