[英]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 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:我有一个
.csv
文件,如下所示:
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):当我返回字母 A 的结果时,我希望它返回这样的数字 (1 + 2):
3
When I return the result for the letter D, I want it to return a number like this (3 + 4):当我返回字母 D 的结果时,我希望它返回这样的数字 (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!
先感谢您!
This is quite easily done using pandas :使用pandas很容易做到这一点:
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: Output:
['A: 3', 'D: 7']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.