熊猫-KeyError:列不在索引中

时间:2018-10-15 12:54:53

标签: python-3.x pandas pandas-groupby

我有一个pandas Dataframe,它具有groupby函数中的列列表。我收到错误消息

KeyError : "['Type1'] not in index"

以下是引发错误的代码

temp_v1 = temp_df.groupby(level, as_index = False).sum()[[level, 'Type1', 'Type2','Type3', 'Type4', 'Type5']]

有人可以指导我上述数据框在哪里出问题。谢谢。

1 个答案:

答案 0 :(得分:1)

我想问题是字符串列Type1

level = 'F'
temp_df = pd.DataFrame({
         'Type1':list('abcdef'),
         'Type2':[4,5,4,5,5,4],
         'Type3':[7,8,9,4,2,3],
         'Type4':[1,3,5,7,1,0],
         'Type5':[5,3,6,9,2,4],
         'col':[5,3,6,9,2,4],
         'F':list('aaabbb')
})

print (temp_df.dtypes)
Type1    object
Type2     int64
Type3     int64
Type4     int64
Type5     int64
col       int64
F        object
dtype: object

解决方案是在sum函数之前添加列表,但在is excluded, because not numeric列中是REST Assured - org.apache.http.NoHttpResponseException: <server_address>:80 failed to respond

Type1

另一个问题是列名或中间空格中有错字,您可以通过将列名转换为cols = [level, 'Type1', 'Type2','Type3', 'Type4', 'Type5'] temp_v1 = temp_df.groupby(level, as_index = False)[cols].sum() print (temp_v1) F Type2 Type3 Type4 Type5 0 a 13 24 9 14 1 b 14 9 8 15 来检查它:

list
相关问题