How can I get the total value count of the next two rows of rows that have more than one same value in a column?

Question:

I want to get the total value count of the next two rows of rows that have more than one same value in a column.

I have a .csv file as follows:

Alphabet Sub alphabet Value
A B 1
A C 2
D B 3
D C 4

When I return the result for the letter A, I want it to return a number like this (1 + 2):

3

When I return the result for the letter D, I want it to return a number like this (3 + 4):

7

When I return to all the letters, I hope it will return to such a list:

['A: 3', 'D: 7']

My code:

import csv

with open("/Users/name/Desktop/path/alphabetical_list.csv") as alphabetical_list_file:
    csv_reader = csv.reader(alphabetical_list_file, delimiter=',')

Feel free to leave a comment if you need more information.

How can I get the total value count of the next two rows of rows that have more than one same value in a column? I would appreciate any help. Thank you in advance!

Asked By: My Car

||

Answers:

This is quite easily done using :

import pandas as pd

out = (pd.read_csv('your_file.csv', sep=',')
         .groupby('Alphabet')['Value']
         .apply(lambda g: f'{g.name}: {g.sum()}')
         .tolist()
       )

Or:

import pandas as pd

out = [f'{name}: {g.sum()}' for name, g in
       pd.read_csv('your_file.csv', sep=',').groupby('Alphabet')['Value']]

Output:

['A: 3', 'D: 7']
Answered By: mozway
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.