Home > database >  Sum the values ​of a column with Python
Sum the values ​of a column with Python

Time:01-31

I'm new to python, I would like to read a column of values ​​from a csv file and add them together, but only those to the left of the "," My csv File:

Name     Code  
Angel    19;90
Eduardo  20;21
Miguel   30;45 

I would like to be able to sum only the numbers to the left of the "Code" column, so that my output is "19 20 30 = 69". I tried deleting the ";" and converting the string to int but sums but joins the numbers together and I have this output:

Your final sum is : 1990  2021  3045 = 7056

CodePudding user response:

If need sum values before ; use Series.str.extract with casting to integers and then sum:

out = df['Code'].str.extract('(.*);', expand=False).astype('int').sum()

Or use Series.str.split with select first values of lists by str[0]:

out = df['Code'].str.split(';').str[0].astype('int').sum()

If need sum all values create DataFrame by expand=True and summing first per rows and then Series:

out = df['Code'].str.split(';', expand=True).astype('int').sum().sum()

If need sum without ; use Series.str.replace:

out = df['Code'].str.replace(';','', regex=True).astype('int').sum()

CodePudding user response:

You can do this like that:

import csv

sum = 0
with open("data.csv", "r") as file:
    reader = csv.reader(file)
    next(reader) # skip header row
    for row in reader:
        code_values = row[1].split(";")
        for value in code_values:
            sum  = int(value)

print(sum)

In line with "split" you can do any kind of splitting logic.

CodePudding user response:

Since it's a calculation thing, here is an approach with :

import numpy as np
​
d = {"left": 0, "right": 1}
choice = "left" # <- choose the position here
​
arr = np.genfromtxt("jacket.csv", delimiter=",", # <- specify a sep
                     dtype=None, skip_header=1, encoding=None)
​
left_vals = np.array([int(code.split(";")[d[choice]]) for code in arr[:,1]])
​
out = np.sum(left_vals)

Output :

print(out)
#69

CodePudding user response:

Just modified ZsoltB's answer :

import csv

sum = 0
with open("demo.csv", "r") as file:
    reader = csv.reader(file)
    next(reader) # skip header row
    for row in reader:
        code_values = row[1].split(";")
        print(code_values[0])
        sum  = int(code_values[0])

print(sum)

The code_values[0] will ensure that you add values only from the left column.

  • Related