1

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()

magicsword
  • 1,179
  • 3
  • 16
  • 26

1 Answers1

1

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

Setup

df = pd.DataFrame(dict(A=list('123'), B=list('232'))
piRSquared
  • 285,575
  • 57
  • 475
  • 624