我有一个excel文件,其中包含3列,如下所示。
Person Item Quantity
John TV 4
John Book 14
John Book 8
John Chair 8
Bob TV 16
Bob Pen 7
Bob Keyboard 8
Bob Pen 7
我需要生成每个人的每个项目的总和。打印出结果如下:
John, TV, 4
John, Book, 22
John, Chair, 8
Bob, TV, 16
Bob, Keyboard, 8
Bob, Pen, 14
我不想要每人需要的物品总数。
目前我正在将CSV文件作为列表进行阅读
import csv
csvfile = open('book1.csv', 'r')
data = csv.reader(csvfile)
for row in data:
如何根据每个人创建项目总和?
答案 0 :(得分:4)
我会使用计数器
from collections import Counter
c = Counter()
for row in data:
try:
name, what, cnt = row
except ValueError: # Empty/broken line
continue
try:
cnt = int(cnt)
except ValueError: # Invalid quantity
continue
c[(name, what)] += cnt
for k in sorted(c):
print "%s, %s, %d" % (k + (c[k], ))
结果:
Bob, Keyboard, 8
Bob, Pen, 14
Bob, TV, 16
John, Book, 22
John, Chair, 8
John, TV, 4
答案 1 :(得分:2)
您也可以使用pandas groupby
import pandas as pd
s = read_excell('/path/to/excell')
s.groupby(level=['Person', 'Item']).sum()
答案 2 :(得分:0)
使用SQLite的力量:
输入:
John,TV,4
John,Book,14
John,Book,8
John,Chair,8
Bob,TV,16
Bob,Pen,7
Bob,Keyboard,8
Bob,Pen,7
sqlite> drop table so;
sqlite> create table so (person text, item text, quantity integer);
sqlite> .mode csv
sqlite> .import so.csv so
sqlite> select person, item, sum(quantity) from so group by person, item;
Bob,Keyboard,8
Bob,Pen,14
Bob,TV,16
John,Book,22
John,Chair,8
John,TV,4
由于Python默认包含,因此可以从Python中完成。
答案 3 :(得分:0)
如果你想把它分成几类,下面是一个尝试: -
import csv
class Items(object):
def __init__(self):
self.Name = None
self.Value = 0
def __init__(self, itm, val):
self.Name = itm
self.Value = int(val)
class People(object):
def __init__(self):
self.Name = None
self.ItemList = []
def __init__(self, name):
self.Name = name
self.ItemList = []
def add_item(self, item, value):
found_item = False
if self.ItemList is not None:
for each_item in self.ItemList:
if each_item.Name == item:
found_item = True
each_item.Value += int(value)
if found_item is False:
self.ItemList.append(Items(item, value))
else:
self.ItemList.append(Items(item, value))
def get_item(self):
if self.ItemList is not None:
for each_item in self.ItemList:
print(self.Name, ",", each_item.Name, ",", each_item.Value)
csvfile = open('book1.csv', 'r')
data = csv.reader(csvfile)
people_list = []
for row in data:
print(row)
found_person = False
if len(people_list) != 0:
for each_person in people_list:
if each_person.Name == row[0]:
found_person = True
each_person.add_item(item=row[1], value=row[2])
if found_person is False:
new_person = People(row[0])
new_person.add_item(item=row[1], value=row[2])
people_list.append(new_person)
else:
new_person = People(row[0])
new_person.add_item(item=row[1], value=row[2])
people_list.append(new_person)
for each_person in people_list:
each_person.get_item()
输出是:
John , TV , 4
John , Book , 22
John , Chair , 8
Bob , TV , 16
Bob , Pen , 14
Bob , Keyboard , 8
答案 4 :(得分:0)
这是一些粗略的伪代码(也称不能保证工作)。但基本上,找到名称和数量的列索引,并在字典中保留每个所有者的记录。
item_ownership_overview = {}
header, data = data[0], data[0:]
name_index = header.index("name")
quantity_index = header.index("quantity")
for row in data:
owner = row[name_index]
quantity = row[quantity_index]
if owner in item_ownership_overview:
item_ownership_overview[owner] += quantity
else:
item_ownership_overview[owner] = quantity