Key error "not in index" when sum a list of columns in pandas
I have a dataframe with about 100 columnns. I want to create a new column with sum of some my columns and i have a list with names of columns to sum.
For example, my columns are: ‘Col1’, ‘Col2’, ‘Col3’, … , ‘Col100’.
and my list of columns to sum is: sumlist = [‘col2’, ‘col17’, ‘col44’, ‘col63’, ‘col72’, ‘col21’, ‘col95’].
List of columns to sum can change so i can’t do something like this:
df[‘total’] = df[‘col2’] + df[‘col17’] + df[‘col44’]
My code: df[‘total’] = df[sumlist].sum(axis=1)
But i have an error all my columns in dataframe are not in index:
KeyError: "[‘Col1’, ‘Col2’, ‘Col3’, … , ‘Col100’] not in index"
Then i try to reset index: df.reset_index() but it doesnt work.
>Solution :
Use Index.intersection:
df = pd.DataFrame({'col2':[5,8],
'col17':[1,2],
'col44':[7,0]})
sumlist = ['col2', 'col17', 'col44', 'col63', 'col72', 'col21', 'col95']
df['total'] = df[df.columns.intersection(sumlist)].sum(axis=1)
print (df)
col2 col17 col44 total
0 5 1 7 13
1 8 2 0 10
Another idea – maybe need capitalize columns names?
sumlist = ['Col2', 'Col17', 'Col44', 'Col63', 'Col72', 'Col21', 'Col95']