【问题标题】:CSV to Multi-Value Dictionary?CSV 到多值字典?
【发布时间】:2019-10-12 21:17:21
【问题描述】:
                Title_1                                Title_2         Type
 He heard it from space  A quick story about sounds from space      Fiction
    The end of all time       A sad poem about the end of time  Non-Fiction
  The perfect beginning               A story about friendship  Non-Fiction

我正在尝试计算所有小说、非小说类型并计算 Title_1 和 Title_2 中相应类型的单词数。

我想要的输出是:

Type         Count  Num-Words  
Non-Fiction   2       20
Fiction       1       12

这是我目前所拥有的:

fopen =  open(file_name, 'r')
fhand = csv.reader(fopen)
next(fhand)
category_sum = dict()
for row in fhand:
    col_0=len(row[0].split())
    col_1=len(row[1].split())
    print( col_1 + col_1)
    if row[2] in category_sum.keys():
        category_sum[row[2]]+=1
    else:
        category_sum[row[2]]=1

我可以在一本不错的字典中获得类型的总数,但我似乎无法弄清楚如何将字数分配给适当的类型作为字典中的值。

有什么想法吗?

【问题讨论】:

    标签: python python-3.x csv dictionary


    【解决方案1】:

    您可以将字典保存为其中一个键为Count 和另一个键为Num-Words 的值。所以你的字典值赋值可能看起来像:

    # num_of_words = 
    if row[2] in category_sum.keys():
        category_sum[row[2]]['Count']+=1
        category_sum[row[2]]['Num-Words']+=num_of_words
    else:
        category_sum[row[2]]={}
    

    【讨论】:

      【解决方案2】:

      你可以这样做:

      import csv
      
      file_name = 'book_titles.csv'
      
      with open(file_name, 'r', newline='') as fopen:
          reader = csv.reader(fopen)
          next(reader)  # Skip header.
          category_sum = {}
          for row in reader:
              category_sum[row[2]] = category_sum.get(row[2], 0) + 1
      
      print(category_sum)  # -> {'Fiction': 1, 'Non-Fiction': 2}
      

      【讨论】:

        【解决方案3】:

        使用pandas:

        • 创建数据框
        • 合并两个标题,按空格分开并计算split创建的列表中的单词
        • groupby on Type,然后聚合 countsum 函数。
          • reset_indexrename 以获得所需的确切形式。
        import pandas as pd
        
        # read the file in
        df = pd.read_csv('file.csv')
        
                        Title_1                                Title_2         Type
         He heard it from space  A quick story about sounds from space      Fiction
            The end of all time       A sad poem about the end of time  Non-Fiction
          The perfect beginning               A story about friendship  Non-Fiction
        
        # count the words in Title_1 & Title_2
        df['num_words'] = df[['Title_1', 'Title_2']].apply(lambda x: len(f'{x[0]} {x[1]}'.split()), axis=1)
        
                        Title_1                                Title_2         Type  num_words
         He heard it from space  A quick story about sounds from space      Fiction         12
            The end of all time       A sad poem about the end of time  Non-Fiction         13
          The perfect beginning               A story about friendship  Non-Fiction          7
        
        # create your desired output
        test = df[['Type', 'num_words']].groupby('Type')['num_words'].agg(['count', 'sum']).reset_index().rename(columns={'count': 'Count', 'sum': 'Num-words'})
        
                Type  Count  Num-words
             Fiction      1         12
         Non-Fiction      2         20
        

        dict 中获取输出:

        test.to_dict('list')
        
        >>> {'Type': ['Fiction', 'Non-Fiction'], 'Count': [1, 2], 'Num-words': [12, 20]}
        

        【讨论】:

          【解决方案4】:

          这是我最终使用的:

          fhand = csv.reader(fopen)
          next(fhand)
          category_sum = dict()
          word_sum = dict()
          
          for row in fhand:
              num_words = len(row[0].split(" ")) + len(row[1].split(" "))
              if row[2] in category_sum.keys():
                  category_sum[row[2]]+=1
                  word_sum[row[2]]+=num_words
              else:
                  category_sum[row[2]]=1
                  word_sum[row[2]]=num_words
          
          
          
          combined = {key:[category_sum[key],word_sum[key]] for key in category_sum}   
          #print(combined)
          print("Category | # Titles | # of Words\n---------------------------------")
          for key in combined:
              print("{}   |   {}  |   {}  ".format(key,combined[key][0],combined[key][1]))
          

          【讨论】:

            猜你喜欢
            • 2021-03-27
            • 2014-12-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-04-13
            • 2013-12-06
            相关资源
            最近更新 更多