简体   繁体   English

KeyError:“键”和TypeError:需要一个整数

[英]KeyError: 'Key' and TypeError: An Integer is required

Both of these questions have been answered I believe but I am currently getting both of these errors in one run and I am not sure what caused it. 我相信已经回答了这两个问题,但是我目前一次都遇到了这两个错误,而且我不确定是什么原因引起的。

# Get data
fileName = 'https://acmsmlblob.blob.core.windows.net/acmsdata/resultExpandedGlbFriendlyName_1.ss_TOP_10000.csv'
raw = pd.read_csv(fileName, ",", header=None)
df = raw.copy()

df['datetime'] = pd.to_datetime(df['StartDateId'], format='%m/%d/%Y %H:%M:%S %p')
df['dow'] = df['datetime'].dt.dayofweek
df['tod'] = df['datetime'].dt.hour
df = df[['dow', 'tod', 'Owner', 'TenantId', 'SplitedPolicy']]

When I run the above code it produces me these errors and since I have absolutely no idea how Python works I am having troubling deciphering why they are thrown. 当我运行上面的代码时,会产生这些错误,并且由于我完全不知道Python的工作方式,所以我很难理解它们为什么被抛出。

TypeError: an integer is required

During handling of the above exception, another exception occurred:

KeyError: 'StartDateId'

Is there any reason why this is happening from the code 是否有任何原因导致代码中发生这种情况

You are telling pandas to ignore the headers on the csv file: 您正在告诉熊猫忽略csv文件上的标头:

raw = pd.read_csv(fileName, ",", header=None)

As a result it doesn't know the column names and wants an integer index. 结果,它不知道列名,而是想要一个整数索引。 The file has a header row, so don't ignore it and you should be able to use the header names: 该文件具有标题行,因此不要忽略它,您应该能够使用标题名:

raw = pd.read_csv(fileName, ",")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM