I have a pd dataframe:
A B
1 2
2 3
3 2
I want: totalA = 1+2+3=6 and totalB = 2+3+2=7
I tried df.sum()
I have a pd dataframe:
A B
1 2
2 3
3 2
I want: totalA = 1+2+3=6 and totalB = 2+3+2=7
I tried df.sum()
Likely, your data is of type str. Try:
df.apply(pd.to_numeric, errors='coerce').sum()
A 6
B 7
dtype: int64
What might happen is the the strings get concatenated to 123
and 232
. But for whatever reason, Pandas casts those to float
even when it started as string.
df.sum()
A 123.0
B 232.0
dtype: float64
df = pd.DataFrame(dict(A=list('123'), B=list('232'))