I have a large df of information that I want to add a 'Total' column to. However, when I use the sum() method the resulting column is populated only with 0 values.
Here is a slice of my df:
COL NAME0 COL NAME1 COL NAME2 COL NAME3 COL NAME4
0 Alabama 4.099099 4.090001 2.042345 NaN
1 Alaska 1.396396 1.390001 1.000000 1.000000
2 Arizona 4.189189 NaN 2.003257 1.537777
3 Arkansas 2.927928 2.920001 2.208723 NaN
4 California 3.378378 3.780001 1.754930 2.012395
To add the Total column, I did the following:
df['Total'] = df.sum(axis=1)
which created the following df:
COL NAME0 COL NAME1 COL NAME2 COL NAME3 COL NAME4 Total
0 Alabama 4.099099 4.090001 2.042345 NaN 0.0
1 Alaska 1.396396 1.390001 1.000000 1.000000 0.0
2 Arizona 4.189189 NaN 2.003257 1.537777 0.0
3 Arkansas 2.927928 2.920001 2.208723 NaN 0.0
4 California 3.378378 3.780001 1.754930 2.012395 0.0
I then tried a different approach, to add each column to the Total column one at a time:
for col in df:
df['Total'] = df['Total'] + df[col]
However, this results in the Total column being populated with only NaN values.
My guess is the existing NaN values in df is causing this behavior in the Total column. This seems to be a simple task so if there's something I'm overlooking let me know. Any suggestions/ solution would be greatly appreciated.