python - key 错误 : 'Key' and TypeError: An Integer is required

标签 python pandas azure

我相信这两个问题都已得到解答,但我目前在一次运行中遇到了这两个错误,我不确定是什么原因造成的。

# 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/

相关文章:

python - 在用户输入时输入 2 个数字,以空格分隔

python - 将点列表分类为形状

python - pytorch中两个一维信号之间的可微卷积

Python:在 pandas GroupBy 对象上有效地使用 apply

python - Pandas 散点图组颜色错误

Python 数量限制

python - 将数据帧列分解为多行(TypeError : Cannot cast array data from dtype ('int64' ) to dtype ('int32' ))

c# - SqlConnectionStringBuilder 类在 dotnetcore 中缺少 Authentication 属性

c# - Azure Web 应用程序上的 Office 组件安装

azure - 可以使用Azure SDK for python来查询azure服务运行状况吗?