【问题标题】:Combine csv_rows in nested dict with counting将嵌套字典中的 csv_rows 与计数相结合
【发布时间】:2023-03-07 21:17:01
【问题描述】:

我想在the dataset 中按月份、形状和参与者计算任何操作的数量。

这是我的代码

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)

结果我得到了一系列的字典。

我怎样才能在一个具有这样结构的嵌套字典中计算这些值?

{'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}

【问题讨论】:

    标签: python csv count readlines


    【解决方案1】:

    这里不需要加入密钥。如果你想要一个嵌套的字典,只需从头开始构建它:

    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
    

    【讨论】:

      【解决方案2】:
      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']}}}})
      

      【讨论】:

        猜你喜欢
        • 2023-01-04
        • 2012-09-20
        • 2019-04-06
        • 2021-04-03
        • 2022-01-19
        • 1970-01-01
        • 2023-03-19
        • 2016-06-09
        • 2019-08-03
        相关资源
        最近更新 更多