解释导入到 CSV 中的 json 数据

问题描述 投票:0回答:1

我有一个从 json 创建的 csv 文件。我的第一栏是我的关键。第一列仅具有唯一值。然后,行中的后续值是属于我的键值的属性的标识代码。对于任何给定行,每行中列出的属性数量范围从属于该键的 1 个属性值到最多 25 个不同的属性值。属性值以无法识别的顺序列出。

一个例子是:

因此,在这个示例中,我基本上想了解“1289679”在数据中出现了多少次,但我需要针对此文件中出现的每一个属性值执行此操作。

非常感谢有关解决此问题的最佳方法的见解。我是做销售的,所以我对此有点不知所措。

谢谢, 马克

我真的还没有尝试过任何东西,因为我通常习惯于使用具有特定列的结构化数据与我这里的数据。道歉。所以基本上我正在寻找的输出是:

json data-structures data-science csvhelper unstructured-data
1个回答
0
投票

csv
模块和
collections.Counter
使这变得非常简单。 扩展可迭代拆包 (PEP 3132) 有助于可变行大小:

import csv
from collections import Counter

counter = Counter()
with open('input.csv', newline='') as fin:
    reader = csv.reader(fin)
    header = next(reader)  # skip header
    # "id" will be the first column in the row.
    # "attributes" will be a list of the remainer of the row.
    for id, *attributes in reader:
        counter.update(attributes)
        #counter.update(set(attributes))  # to only count once per row if needed

with open('output.csv', 'w', newline='') as fout:
    writer = csv.writer(fout)
    writer.writerow(['Attribute Value', 'Count of Appearances'])  # header
    for attribute, count in counter.items():
        writer.writerow([attribute, count])

input.csv(由于数据是图像而设计的示例):

ID,Attributes
abc,1,2,3,3,3,3
bcd,2,3,4
cde,4,5,6,7,8
def,1,2,3,4,5,6,7,8,9,10

输出.csv:

Attribute Value,Count of Appearances
1,2
2,3
3,6
4,3
5,2
6,2
7,2
8,2
9,1
10,1
© www.soinside.com 2019 - 2024. All rights reserved.