[英]storing .csv data in Python
I have two variables – animals
and food
; 我有两个变量–
animals
和food
; if I print them they look like 如果我打印它们,它们看起来像
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
and so on... 等等...
I want this data to be stored in a new CSV file in the following format (but can't figure out how to do it): 我希望将这些数据以以下格式存储在新的CSV文件中(但不知道该怎么做):
animals food count
pig acorn 15
pig carrot 7
pig potato 10
dog acorn 2
dog meat 10
dog potato 1
and so on... In other words, I want the observation in the animals
variable to reoccur exactly as many times as there are different types of items in the food
variable, and place the aggregated score in a new variable. 依此类推……换句话说,我希望对
animals
变量进行的观察准确地重复发生,就像在food
变量中存在不同类型的项目一样,并将汇总分数放入新变量中。 Eg, if there are 50 occurrences of pig
, 30 of which are acorn
, 10 of which are carrot
and 10 potato
, I would like it to look like this: 例如,如果出现50
pig
,其中30头是acorn
,其中10头是carrot
和10 potato
,我希望它看起来像这样:
pig acorn 30
pig carrot 10
pig potato 10
First of all - this has little to do with CSV itself. 首先-这与CSV本身无关。 If you want to count the values like here, using a dictionary is a good idea, so what you need is something like (i assume that animals and foods are lists):
如果您想像这样计算值,使用字典是个好主意,那么您需要的是类似的东西(我假设动物和食物都是清单):
counts = {}
for animal, food in zip(animals, foods):
counts.setdefault((animal, food), 0)
counts[(animal, food)] += 1
After this loop you will have a dictionary with keys that are (animal, food) tuples and values that are the counts. 在此循环之后,您将拥有一个字典,其中包含(动物,食物)元组的键和作为计数的值。 So you can write them to csv like:
因此,您可以像这样将它们写入csv:
for ((animal, food), count) in counts.items():
csv_writer.writerow([animal, food, count])
It looks that you do not know the wonderful Counter
class of collections
. 看来您不知道精彩的
Counter
类collections
。 Here is the documentation . 这是文档 。
If you want to count your variable pairs: 如果要计算变量对:
c = Counter(zip(var1, var2))
to write the results, use the csv
library, as reported in zetciu answer, but remember that Counter instances are dict
. 要编写结果,请使用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.