5

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
2
  • expected ouput is 69 ? Or 7056 ? Commented Jan 31, 2023 at 11:37
  • 1
    output is 69, @jezrael Commented Jan 31, 2023 at 11:54

4 Answers 4

5

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()
Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

I tried this code but it gives me the following error :IndexError: list index out of range
1

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.

4 Comments

This code takes the data by row, I need it to add by column
You want to add 19 + 20 + 30 right ?
yes, yep, correct
The above code does the same. I created a demo.csv file and it worked.
0

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

2 Comments

Hi @Timeless, i try your code and i have this error : IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
AFK now, let me check that later ;). Anyways, for the sample you shared, the code should work.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.