将嵌套dict中的csv_rows与计数相结合



我想计算数据集中任何动作的数量,除以月份、形状和参与者。

这是我的代码

import csv
count_dict = dict()
with open(r'st2.csv',
mode = 'r') as csv_file:

lines = csv_file.readlines()

for row in lines:

data = row.split(',')
key = data[1] + 't' + data[2] + 't' + data[3][:7] + 't' + data[4].strip()
if key in count_dict:
count_dict[key] += 1
else:
count_dict[key] = 1
print('t'.join(['Name', 'Shape', 'Month', 'Action', 'Count']))
for element, count in count_dict.items():
items = element.split('t')
activities = dict()
curdict = activities

for chunk in items:
curdict[chunk] = dict()
curdict = curdict[chunk]

for i in activities:
for k in activities[i]:
for l in activities[i][k]:
for m in activities[i][k][l]:
activities[i][k][l][m] = count

print(activities)

结果我得到了一系列的格言。

我怎么能在一个嵌套dict中计算这样的值呢

{'Googenhaim': {'Circle': {'2020-04': {'pin': 0, 'tap': 0, 'throw': 0, 'play': 1}, '2020-06': {'pin': 0, 'tap': 0, 'throw': 0, 'play': 1}, '2020-05': {'pin': 0, 'tap': 0, 'throw': 0, 'play': 1}}, 'Rectangle': {'2020-04': {'pin': 0, 'tap': 0, 'throw': 0, 'play': 1}, '2020-06': {'pin': 0, 'tap': 0, 'throw': 0, 'play': 1}, '2020-05': {'pin': 0, 'tap': 0, 'throw': 0, 'play': 1}}, 'Trapezoid': {'2020-04': {'pin': 0, 'tap': 0, 'throw': 0, 'play': 1}, '2020-06': {'pin': 0, 'tap': 0, 'throw': 0, 'play': 1}

这里不需要连接键。如果你想要一个嵌套的dict,只需从头开始构建它:

count_dict = dict()
keys = ['pin', 'play', 'tap', 'throw']
with open('st2.csv') as csv_file:
rd = csv.reader(csv_file)
for row in rd:
if not row[1] in count_dict:
count_dict[row[1]] = {}
loc = count_dict[row[1]]
if not row[2] in loc:
loc[row[2]] = {}
dat = loc[row[2]]
if not row[3] in dat:
dat[row[3]] = {k: 0 for k in keys}
typ = dat[row[3]]
typ[row[4]] += 1
import pandas as pd
df = pd.read_csv('data.csv', sep='t', names=['id','name','shape','date','move'])
df['count'] =df.groupby(['name','shape','date'])['move'].transform('count')
df = df.pivot_table(index=['name','shape','date'],columns='move',values='count', fill_value=0).reset_index()
l = []
for index, r in df.iterrows():
l.append({r['name']:{r['shape']:{r['date']:{'pin':r['pin'], 'tap':r['tap'], 'throw':r['throw'], 'play':r['play']}}}})

最新更新