0

I have the value placed in the data frame.

abc = data
abc = pd.DataFrame(abc)

total = abc['profit'].sum()
print(total)

output of print(abc['profit'])

0     -720.00
1    2,620.00
Name: m2m, dtype: object

total = abc['profit'].sum()
print(total)

The output is showing like this: -720.002,620.00

I want value should be the sum and provide one output.

looking for help,

Axisnix
  • 2,822
  • 5
  • 19
  • 41
raju kiranlr
  • 15
  • 1
  • 4
  • 1
    you have to convert to numerical values first. – rhug123 Feb 02 '21 at 16:08
  • use the `thousands` argument in `pd.read_csv` `from io import StringIO`; `d = """0 -720.00 1 2,620.00 """`;`df = pd.read_csv(StringIO(d),sep='\s+',header=None,thousands=',')` – Umar.H Feb 02 '21 at 16:15
  • Does this answer your question? [pandas reading CSV data formatted with comma for thousands separator](https://stackoverflow.com/questions/37439933/pandas-reading-csv-data-formatted-with-comma-for-thousands-separator) – Umar.H Feb 02 '21 at 16:17

1 Answers1

0

sum() is defined as concatenation for strings. Convert to float and you get what you expect.

df = pd.DataFrame({"profit":["-720.00","2,620.00"]})

print(df["profit"].sum())
print(df["profit"].str.replace(",","").astype(float).sum())

output

-720.002,620.00
1900.0
Rob Raymond
  • 29,118
  • 3
  • 14
  • 30