Python从CSV文件在一个字典中添加多个数据点

马修·詹金森(Mathew Jenkinson)

我有一个CSV文件,看起来像:

CountryCode, NumberCalled, CallPrice, CallDuration
BS,+1234567,0.20250,29
BS,+19876544,0.20250,1
US,+121234,0.01250,4
US,+1543215,0.01250,39
US,+145678,0.01250,11
US,+18765678,None,0

我希望能够分析文件以从数据中获取一些统计信息:

CountryCode, NumberOfTimesCalled, TotalPrice, TotalCallDuration
US, 4, 1.555, 54

目前,我有dict多数民众赞成在设置:

CalledStatistics = {}

当我从CSV读取每一行时,将数据放入dict的最佳方法是什么?

CalledStatistics['CountryCode'] = {'CallDuration', 'CallPrice', 'NumberOfTimesCalled'}

添加第二条美国行会覆盖第一行还是会基于键“ CountryCode”添加数据?

泽佐洛

这些调用中的每一个:

CalledStatistics['CountryCode'] = {'CallDuration', 'CallPrice', 'NumberOfTimesCalled'}

会覆盖之前的通话。

为了计算所需的总和,您可以使用dict的dict。就像在for循环中,您将数据包含在以下变量中:country_code,call_duration,call_price以及将数据存储在collected_statistics中的位置:(编辑:添加了第一行,以便在将call_price记录为None的情况下将其变为0数据;这段代码旨在处理一致的数据(仅像整数一样),如果可能还有其他类型的数据,则需要将它们转换为整数(或相同类型的任何数字),然后python才能将它们求和)

call_price = call_price if call_price != None else 0

if country_code not in collected_statistics:
    collected_statistics[country_code] = {'CallDuration' : [call_duration],
                                          'CallPrice' : [call_price]}
else:
    collected_statistics[country_code]['CallDuration'] += [call_duration]
    collected_statistics[country_code]['CallPrice'] += [call_price]

在循环之后,对于每个country_code:

number_of_times_called[country_code] = len(collected_statistics[country_code]['CallDuration']

total_call_duration[country_code] = sum(collected_statistics[country_code]['CallDuration'])
total_price[country_code] = sum(collected_statistics[country_code]['CallPrice'])

好的,所以最后这是一个完整的工作脚本,可以处理您给出的示例:

#!/usr/bin/env python3

import csv
import decimal

with open('CalledData', newline='') as csvfile:
    csv_r = csv.reader(csvfile, delimiter=',', quotechar='|')

    # btw this creates a dict, not a set
    collected_statistics = {}

    for row in csv_r:

        [country_code, number_called, call_price, call_duration] = row

        # Only to avoid the first line, but would be better to have a list of available
        # (and correct) codes, and check if the country_code belongs to this list:
        if country_code != 'CountryCode':

            call_price = call_price if call_price != 'None' else 0

            if country_code not in collected_statistics:
                collected_statistics[country_code] = {'CallDuration' : [int(call_duration)],
                                                      'CallPrice' : [decimal.Decimal(call_price)]}
            else:
                collected_statistics[country_code]['CallDuration'] += [int(call_duration)]
                collected_statistics[country_code]['CallPrice'] += [decimal.Decimal(call_price)]


    for country_code in collected_statistics:
        print(str(country_code) + ":")
        print("number of times called: " + str(len(collected_statistics[country_code]['CallDuration'])))
        print("total price: " + str(sum(collected_statistics[country_code]['CallPrice'])))
        print("total call duration: " + str(sum(collected_statistics[country_code]['CallDuration'])))

使用CalledData作为具有与您提供的内容完全相同的文件的文件,它将输出:

$ ./test_script
BS:
number of times called: 2
total price: 0.40500
total call duration: 30
US:
number of times called: 4
total price: 0.03750
total call duration: 54

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将一个字典添加到数据框列?

在python中按顺序将多个字典附加到一个字典中

如果一个字段不存在,Mongodb将添加多个文档

使用一个.map调用使用一个字典更改数据框中的多个系列/列的值

如何在Asp.Net Core Web App中向模型的一个字段添加多个值?

将具有相同键值对的多个字典合并到一个字典python

如何在Django模型的一个字段中添加多个输入

Python从另一个字典列表更新字典列表中的值

在Swift中从两个数组生成一个字典数组

如何在一个字符串(URL)中添加多个变量

Firestore云功能在一个字段中添加多个字段

在python 3的另一个字典中添加带有静态键的字典

python数据结构:字典到一个字典的列表

在一个字典python中从两个数组中添加值的最佳方法

从另一个字典中删除字典(python 3中的奇怪行为)

遍历一个字典。python中的key()

将两个字典值合并为一个,然后将其添加到Python中的另一个字典中

在 Python 中迭代一个字典键的多个值

PHP 在一个字符串中的多个位置添加多个字符串

Django 一个字段中的多个文件

Python3:将一个字典中每个键的多个值除以另一字典中每个键的多个值

Firestore 在同一个 UID 下添加多个数据

如何在 Python 中使用来自一个 Excel 列的多个数据点?

代码返回一个字典,如何获取这个数据?

在 Python 中添加多个字典列表的内容

如何在一个字符串中添加多个数字(python)

将 Python 字典插入另一个字典,生成 .plist 文件?

向列中的某些数据点添加一个字母

如何将字典中每个数据帧的列附加到另一个字典中的另一个数据帧?