我相信这两个问题都已得到解答,但我目前在一次运行中遇到了这两个错误,我不确定是什么原因造成的。
# 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']]
当我运行上面的代码时,它会产生这些错误,并且由于我完全不知道 Python 是如何工作的,所以我很难解释为什么会抛出这些错误。
TypeError: an integer is required
During handling of the above exception, another exception occurred:
KeyError: 'StartDateId'
代码中发生这种情况有什么原因吗
最佳答案
您告诉 pandas 忽略 csv 文件上的 header :
raw = pd.read_csv(fileName, ",", header=None)
因此,它不知道列名并且需要整数索引。该文件有一个标题行,因此不要忽略它,您应该能够使用标题名称:
raw = pd.read_csv(fileName, ",")
关于python - key 错误 : 'Key' and TypeError: An Integer is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57281635/