Reputation: 3719
I have a pandas Dataframe that has a list of columns in a groupby function. I get an error
KeyError : "['Type1'] not in index"
Given below is the code that throws the error
temp_v1 = temp_df.groupby(level, as_index = False).sum()[[level, 'Type1', 'Type2','Type3', 'Type4', 'Type5']]
Could anyone guide me where am I going wrong with the above Dataframe. Thanks..
Upvotes: 0
Views: 1380
Reputation: 862661
I guess problem is string column 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
Solution is add list before sum
function, but Type1
column is excluded, because not numeric.
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
Another problem is typo in column name or traling whitespace, you can check it by converting columns names to list
:
print (temp_df.columns.tolist())
['Type1', 'Type2', 'Type3', 'Type4', 'Type5', 'col', 'F']
Upvotes: 2