获取每个con的唯一custId数

2024-10-03 09:17:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我在下面数据.csv你知道吗

custId,contract,zone,teamcode,projectcode,time
2,2345,us_east,Red,A,5s
1,2345,us_west,Blue,B,1s
2,2346,eu_west,Yellow,C,2s
1,2345,us_west,Blue,D,1s
3,2346,eu_west,Yellow,E,2s

我不想在这里用熊猫。你知道吗

我是python的新手,我不知道如何解决这个问题。我设法用csv读取数据,但我不知道下一步该怎么做。你知道吗

import csv
with open('data.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)

编辑:我需要找到每个合同的唯一custId编号。你知道吗


Tags: csv数据zonetimebluereaderrowus
2条回答

我想你应该计算一下每个合同中有多少客户。你知道吗

如果是这样的话,那么这里是如何做到这一点,不使用熊猫

import csv

file = open('data.csv', 'r')
reader = csv.reader(f)

# We create a list of all unique contracts
contracts = set([row[1] for row in reader])

# We create an array that will contain how many customers in each contract
array = []

# For each contract
for contract in contracts:

    # We initialize the number of customers
    count = 0

    # We loop through the lines
    for row in reader:

        row_contract = row[1]

        # If we find a line containing the contract
        if row_contract  == contract:

            # We increment the number of customers for the current contract
            count += 1

    array.append([contract, count])

输出:

[[2345, 3], [2346, 2]]

看起来你需要collections.defaultdictset。你知道吗

例如:

import csv
from collections import defaultdict
unique_C = defaultdict(set)
with open(filename, 'rU') as f:
    reader = csv.reader(f)
    next(reader)   #Skip header
    for row in reader:
        unique_C[row[1]].add(row[0])
print(unique_C)

输出:

defaultdict(<type 'set'>, {'2345': set(['1', '2']), '2346': set(['3', '2'])})

相关问题 更多 >