[英]storing .csv data in Python
我有两个变量– animals
和food
; 如果我打印它们,它们看起来像
var1 var2
pig acorn
pig acorn
pig carrot
pig potato
pig acorn
pig carrot
dog meat
dog acorn
dog carrot
dog potato
dog carrot
dog meat
cat meat
cat fish
cat carrot
cat potato
等等...
我希望将这些数据以以下格式存储在新的CSV文件中(但不知道该怎么做):
animals food count
pig acorn 15
pig carrot 7
pig potato 10
dog acorn 2
dog meat 10
dog potato 1
依此类推……换句话说,我希望对animals
变量进行的观察准确地重复发生,就像在food
变量中存在不同类型的项目一样,并将汇总分数放入新变量中。 例如,如果出现50 pig
,其中30头是acorn
,其中10头是carrot
和10 potato
,我希望它看起来像这样:
pig acorn 30
pig carrot 10
pig potato 10
首先-这与CSV本身无关。 如果您想像这样计算值,使用字典是个好主意,那么您需要的是类似的东西(我假设动物和食物都是清单):
counts = {}
for animal, food in zip(animals, foods):
counts.setdefault((animal, food), 0)
counts[(animal, food)] += 1
在此循环之后,您将拥有一个字典,其中包含(动物,食物)元组的键和作为计数的值。 因此,您可以像这样将它们写入csv:
for ((animal, food), count) in counts.items():
csv_writer.writerow([animal, food, count])
看来您不知道精彩的Counter
类collections
。 这是文档 。
如果要计算变量对:
c = Counter(zip(var1, var2))
要编写结果,请使用zetciu答案中报告的csv
库,但请记住Counter实例是dict
。
with open('result.csv', 'wb') as csvfile:
csv_writer = csv.writer(csvfile)
csv_writer.writerow(["animals", "food", "count"])
for pair,count in c.items():
animal, food = pair
csv_writer.writerow([animal, food, count])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.