简体   繁体   English

python / excel:如何为列A中的所有相同值添加列B的值

[英]python/excel: How do I add values of column B for all the same values in column A

Excel Image 1 Excel图片1

Excel Image 2 Excel图片2

Please look at the "Excel Image 1" to start. 请查看“ Excel Image 1”开始。

I am using python to pull those first two columns, and storing them in their own separate array. 我正在使用python提取前两列,并将它们存储在自己的单独数组中。 What I want to do is add the values of column B if the value in column are the same. 我想做的是如果列中的值相同,则添加列B的值。 For ex: if the value in column A is "0", then add -200 and 400 and get 200 because "0" is in row 1 and row 2 of column A. 例如:如果A列中的值为“ 0”,则将-200和400相加并得到200,因为“ 0”位于A列的第1行和第2行中。

After the addition as shown in "Excel Image 2", store the new values of both the Column in their own separate arrays, so I can use them for later calculations. 如“ Excel Image 2”中所示添加后,将两个Column的新值存储在它们自己的单独数组中,因此我可以将它们用于以后的计算。

import xlrd
excel = '/Users/Bob/Desktop/'

wb1 = xlrd.open_workbook(excel + 'assignment.xlsx')
sh1 = wb1.sheet_by_index(0)

colA,colB = [],[]
for a in range(0,sh1.nrows):
    colA.append(int(sh1.cell(a,0).value))
    colB.append(int(sh1.cell(a,1).value))
print(colA)
print(colB)

for i in colA:
    if i == 0:
        add = colB[0] + colB[1]
print(add)

I want a code that adds those values in column B irrespective of how many number or number of same values are given in column A. 我想要一个代码,将这些值添加到B列中,而不管A列中给出了多少个相同值。

Current output: 电流输出:

[0, 0, 1, 2, 2, 2, 3, 3, 4, 4]
[-200, 400, 30, 600, -70, 10, 20, -90, 40, 40]

Expected output: 预期产量:

[0, 1, 2, 3, 4]
[200, 30, 540, 70, 80]

Thanks! 谢谢!

Use itertools.groupby() : 使用itertools.groupby()

from itertools import groupby
import xlrd
excel = '/Users/Bob/Desktop/'

wb1 = xlrd.open_workbook(excel + 'assignment.xlsx')
sh1 = wb1.sheet_by_index(0)

sheet_rows = [sh1.row(r) for r in range(0, sh1.nrows)]
groups = groupby(sheet_rows, lambda row: row[1])

key_list = []
sum_list = []
for key, group in groups:
    key_list.append(key)
    sum_list.append(sum(group))

print(key_list)
print(sum_list)

I'd use the row_values method, instead. 我将使用row_values方法。 This is called with the row number and the column number (zero-based). 这称为行号和列号(从零开始)。 For example to get the first row in a list do this: 例如,要获取列表中的第一行,请执行以下操作:

first_row = sh1.row_values(0)
[0.0, -200.0]

Slice the list to get values from specific columns. 切片列表以从特定列获取值。 Such as: 如:

sh1.row_values(0)[1]
-200.0

I started by keeping track of the values in the first column ( curr_key ) and running a cumulative sum for a given key— cum_sum . 我首先跟踪第一列( curr_key )中的值,然后为给定键cum_sum运行累积总和。 When the key changes, I just update append the cum_sum to sum_list and update curr_key and cum_sum . 当密钥更改时,我只是将cum_sum追加到sum_list并更新curr_keycum_sum The last thing to do is to append the last value in the cumulative sum. 最后要做的是将最后一个值附加到累积和中。

curr_key = sh1.row_values(0)[0]
cum_sum = sh1.row_values(0)[1]
sum_list = []
for a in range(1,sh1.nrows):
    if sh1.row_values(a)[0] == curr_key:
        cum_sum += sh1.row_values(a)[1]
        curr_key = sh1.row_values(a)[0]
    else:
        sum_list.append(cum_sum)
        curr_key = sh1.row_values(a)[0]
        cum_sum = sh1.row_values(a)[1]
sum_list.append(cum_sum)

This yields the following in sum_list : 这将在sum_list产生以下sum_list

[200.0, 30.0, 540.0, -70.0, 80.0]

BTW, your second file has 70.0 instead of -70.0 in row 3. 顺便说一句,您的第二个文件在第3行中的值为70.0,而不是-70.0。

After some thought I realized a better way was to use a dictionary (similar in concept to @Austin Hastings answer, but without the groupby ). 经过一番思考,我意识到一种更好的方法是使用字典(概念类似于@Austin Hastings的答案,但没有groupby )。

key_vals = [x for x in sh1.col_values(0)]
int_vals = [y for y in sh1.col_values(1)]
sum_list = {}
for i,value in enumerate(key_vals):
    if str(value) not in sum_list:
        sum_list[str(value)] = int_vals[i]
    else:
        sum_list[str(value)] += int_vals[i]
for key in sorted(sum_list):
    print('{}, {}'.format(key, sum_list[key]))

This yields: 这样产生:

0.0, 200.0
1.0, 30.0
2.0, 540.0
3.0, -70.0
4.0, 80.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM