-1

I am simply trying to get the sum of a column ('Value') in a dataframe in python. I believe I am creating the dataframe, but I am not sure. I am getting a result back, however, it's not the result I imagine when it comes to .sum().

import csv
import pandas as pd

with open('new_taste.csv', 'r') as csv_trades:
    csv_reader = csv.DictReader(csv_trades)

    df = pd.DataFrame(data=csv_reader)
    total = df['Value'].sum()
    df.loc['Value'] = pd.Series(df['Value'].sum(), index=['Value'])
    print(df['Value'])

is giving me

0                                                      133
1                                                      180
2                                                     -117
3                                                     -159
4                                                        1
                               ...                        
316                                                     14
317                                                     14
318                                                     19
319                                                     18
Value    133180-117-15911-84-3-10-23434245-23-3085-5241...
Name: Value, Length: 321, dtype: object

I noticed it's bringing back every number, not the sum of the numbers. I would like to bring back the sum.

5
  • Have you done any debugging? It’s like 10 lines of code. In any case, look at the last line of the output you shared, there’s a pretty big hint at what the issue is.
    – AMC
    Feb 17, 2020 at 4:15
  • Also, Pandas can read CSV files, and you probably don’t need to make that Series. You create a variable total to hold the sum, but then recalculate it anyway.
    – AMC
    Feb 17, 2020 at 4:35
  • The series was to see why I was getting a very long string of numbers and it occurred to me that I was see the numbers in Value all together. At first, I didn't know if it was grabbing from the other columns which you don't see. This allowed me to ask a more succinct question to see what I was missing, and it was a very simple thing: float. I don't have the best debugging habits.
    – Luck Box
    Feb 17, 2020 at 4:42
  • The series was to see why I was getting....in Value all together. Sorry, I’m not sure I understand what you mean. Can you put it differently, maybe?
    – AMC
    Feb 17, 2020 at 4:44
  • When I print just the total function it only came back with just one line. Since I have a bunch of other columns that you don't see in my example, I wasn't sure if I was grabbing from the right column, as I'm still very much learning. So, I brought in the series and simply explained how I arrived where I was. This is my debugging method and it's why you see two lines of code doing the same thing.
    – Luck Box
    Feb 17, 2020 at 4:55

2 Answers 2

1

It seems return string values after recognizing df['Value'] values to string.

How about change 9th line of your code to below

df.loc['Value'] = pd.Series(df['Value'].sum(), index=['Value'])

And could you share what it returns to "total" variable.

2
  • Thanks for throwing out the string values thing. I was able to find the fix based on what you said, even though the code snippet you provided was the same as what I had originally posted.
    – Luck Box
    Feb 17, 2020 at 2:43
  • Anyway glad to help you haha
    – puhuk
    Feb 17, 2020 at 2:59
0

I think this is my answer. I have to go through and check the math cause I believe I have to further tweak what I'm adding up. I was returning a string.

import csv
import pandas as pd

with open('new_taste.csv', 'r') as csv_trades:
    csv_reader = csv.DictReader(csv_trades)

    df = pd.DataFrame(data=csv_reader)
    total = df['Value'].sum()
    df.loc['Value'] = pd.Series(df['Value'].astype(float).sum(), index=['Value'])
    print(df['Value'])

print(total)

2
  • Read the CSV file using Pandas, you can specify the data type there if it fails to infer it properly. You know that you’re creating a row to store the sum, right? That doesn’t seem ideal.
    – AMC
    Feb 17, 2020 at 4:55
  • I've only been looking into csv and pandas the last few days, not having looked at python for about a year now.
    – Luck Box
    Feb 17, 2020 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.