Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Key error "not in index" when sum a list of columns in pandas

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading