简体   繁体   English

如何获得在一列中具有多个相同值的下两行行的总值计数?

[英]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 1个
A一种 C C 2 2个
D B 3 3个
D C C 4 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 :使用很容易做到这一点:

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.

相关问题 Pandas:如果两行或多行在特定列中具有相同的值,则获取其计数并添加到下一行 - Pandas: If two or more rows have same values in particular column then get its count and add to next row 如何在熊猫数据框中找到与另一列中的多个值相对应的列中具有值的所有行? - How can I find all rows with a value in one column which corresponds to more than one value in another column in a pandas dataframe? 如何检查 pandas 列中接下来的 3 个连续行是否具有相同的值? - How to check if next 3 consecutive rows in pandas column have same value? 如果值在列中重复超过两次,如何删除行 - How to delete rows if the value duplicated more than two times in a column 如何对第 1 列中具有相同值的两个或多个 csv 文件的行求和? - How to sum rows of two or more csv files that have the same value in column 1? Pandas Dataframe,当某些行可能超过1“时,如何将列拆分为”,“ - Pandas Dataframe, How can I split a column into two by “,” when some rows may have more than 1 “,” 如何比较两个相同大小的数据框并创建一个新的数据框,而在列中没有具有相同值的行 - How to compare two dataframes of the same size and create a new one without the rows that have the same value in a column 如何计算每列中有多少行的值等于 3 并将它们存储在新列中 - How can I count how many rows have a value equal to 3 in each column and store them in a new column 如何组合 pandas dataframe 中在一列中具有相同值的行 - How to combine rows in a pandas dataframe that have the same value in one column 返回在一列中具有相同值但列值未知的行 - Return rows that have the same value in one column but the column value is unknown
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM