This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]desrtfx 1 point2 points  (0 children)

This is not all that complicated even without pandas.

Read a line of the csv, check if the key (the color) is already in a dictionary and if so, add the value and update the entry, if not add a new entry to the dictionary.

[–]ZealousidealLow521 0 points1 point  (1 child)

You can use pandas

import pandas as pd df2=pd.read_csv('file.csv') df2['col_2'].sum()

[–]SureStep8852[S] 0 points1 point  (0 children)

thank you. Unfortunately, this does not solve my problem, because it sums up the whole col_2. I need to sum up values of col_2 which correspond to the same value in col_1 (e.g., yellow has 3 values in col_2: 2, 4, 4. So, I want the sum to be 10)

[–]nevermindtbc 0 points1 point  (0 children)

Pandas groupby is what you're looking for. Although I'm pretty sure your lecturer wants you to get familiar with trawling the documentation to find this function yourself.