Search code examples
pandaslistsum

Adding multiple columns from a pandas data frame into a new column


I ran some code which resulted in either a 1 if something happened or a 0 if it did not. The results are stored in 8 separate columns (count600, count800, etc) which visual studio has as a list type. I then added the 8 columns together and have the results appear in a new column titled SUM_OVER90th. The value stored in the SUM_OVER90th appears like 11111.0 instead of the value 5. I tried a few different scripts which all seem to give a similar result. I'm not sure why I'm not able to add these column together. Thanks for any advice on what I might be doing wrong!

COLS_TO_ADD = ['600_COUNT','800_COUNT','1000_COUNT','1200_COUNT','1400_COUNT','1600_COUNT','1800_COUNT','2000_COUNT']
Q_ETL_sub['SUM_OVER90th'] = Q_ETL_sub[COLS_TO_ADD].sum(axis=1)

enter image description here


Solution

  • One possible reason is that your columns to add are strings, not numbers. You can verify this with

    Q_ETL_sub.dtypes
    

    and if they are not numbers, try convert them into integer with

    Q_ETL_sub[COLS_TO_ADD] = Q_ETL_sub[COLS_TO_ADD].astype(int)