【问题标题】:file csv: how can I count occourrences of value based on other column value with python?文件 csv:如何使用 python 根据其他列值计算值的出现次数?
【发布时间】:2020-05-15 06:41:53
【问题描述】:

我有一个 .csv 文件(600 行),其中包含一些字段:提交 ID、气味类型等。

我会为每个提交 ID 计算每种气味的出现次数。

我会输出的示例:

   commit dfbu3u4498fbbefi: [dense structure :1, cyclic dependency:4, unstable dependency: 67, feature concentration: 6, god component: 8]
  commit  bifueifyuwefbvwr: [dense structure :34, cyclic dependency:43, unstable dependency: 97, feature concentration: 43, god component: 10]

我试过这个,但我认为我需要另一个循环(也许?) 抱歉,我之前没用过 Python

import csv
import collections

smell = collections.Counter()


with open('Ref.csv') as file:
    reader = csv.reader(file, delimiter=';')

    for row in reader:

        smell[row[0]] += 1

print (smell.most_common(5))

OUTPUT:

[('9b0dd5dc979bd490ae34f6d790c466b47c84c920', 96), ('6431099fe7d5d90da678a78051f12894da82c68d', 96), ('44fdfa7ea93c15bb116a25e0675d98469deafaa6', 96), ('b2c40612a2c60685555f35af71f5801391a58b4b', 96), ('aa6cbb78cca17a9de339b2d060c00352e8beedde', 96)]

or if i change row index to 2 i got

[('Unstable Dependency', 315), ('Feature Concentration', 238), ('God Component', 84), ('Cyclic Dependency', 28), ('Dense Structure', 7)]

【问题讨论】:

  • 到底是什么问题?你在哪方面苦苦挣扎?
  • df.groupby(['commit_id', 'smell']).count().
  • 到目前为止你尝试过什么?你能分享你的代码并详细解释你的评论吗?请参阅@sammywemmy 提到的文档。
  • @Iv10 谢谢,我编辑了问题

标签: python pandas loops csv


【解决方案1】:

你可以使用pandas来做:

import pandas as pd

# Dataframe definition
df = pd.read_csv('Ref.csv', sep=';')

# Group and get the count values.

df_grouped = df.groupby(by=['commit', 'smell']).size()

df_grouped 现在是 pandas.series,如果您希望它再次成为 dataframe,您应该这样做:

df_grouped = df_grouped.reset_index()
df_grouped = df_grouped.rename(columns={0: "counts"})

我强烈建议您查看文档:https://pandas.pydata.org/pandas-docs/stable/index.html

【讨论】:

  • 我试过但失败了:(我得到了这个runfile('C:/Users/daisy/.spyder-py3/untitled1.py', wdir='C:/Users/daisy/.spyder-py3') Traceback (most recent call last): File "<ipython-input-3-fbb006e8e2f7>", line 1, in <module> runfile('C:/Users/daisy/.spyder-py3/untitled1.py', wdir='C:/Users/daisy/.spyder-py3') File "C:\Users\daisy\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace)
  • File "C:\Users\daisy\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/daisy/.spyder-py3/untitled1.py", line 8, in <module> df_grouped = df.groupby(by=['commit', 'smell']).size() File "C:\Users\daisy\Anaconda3\lib\site-packages\pandas\core\generic.py", line 7894, in groupby **kwargs
  • File "C:\Users\daisy\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py", line 2522, in groupby return klass(obj, by, **kwds) File "C:\Users\daisy\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py", line 391, in __init__ mutated=self.mutated, File "C:\Users\daisy\Anaconda3\lib\site-packages\pandas\core\groupby\grouper.py", line 621, in _get_grouper raise KeyError(gpr) KeyError: 'commit'
  • 抱歉,我在列名中使用了大写字母。我已经编辑了我的答案
  • 也许你的列有不同的名字,你能告诉我df.head()的输出吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-30
相关资源
最近更新 更多