Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算CSV文件中的前导数字?_Python_Python 3.x_Csv - Fatal编程技术网

Python 计算CSV文件中的前导数字?

Python 计算CSV文件中的前导数字?,python,python-3.x,csv,Python,Python 3.x,Csv,我有一个CSV文件,在第1列中有以下值: 值 170 900 250 450 125 994 ..... 我想循环遍历文件中的所有行(不包括值标题),并获得每个值的每个前导数字(从1到9)的出现次数,例如第一个数字为1的值的数量是2(170和125)。这适用于任何长度的数字: from collections import Counter rows = [170, 900, 200, 200, 300, 100, 293] leading = list(map(lambda x: int(

我有一个CSV文件,在第1列中有以下值:

值
170
900
250
450
125
994
.....

我想循环遍历文件中的所有行(不包括值标题),并获得每个值的每个前导数字(从1到9)的出现次数,例如第一个数字为1的值的数量是2(170和125)。

这适用于任何长度的数字:

from collections import Counter

rows = [170, 900, 200, 200, 300, 100, 293]

leading = list(map(lambda x: int(str(x)[0]), rows))
print(Counter(leading))
如果数字长度正好为3位数,则可以执行以下操作:

leading = list(map(lambda x: x//100, rows))
印刷品:

Counter({2: 3, 1: 2, 9: 1, 3: 1})

然后,如果需要实际的dictionary对象,可以对Counter对象调用
dict()

一种方法是将
csv
模块与
collections.Counter一起使用

结果是一个
value:count
项的字典

import csv 
from collections import Counter

# read in data as list, excluding first row
with open('file.csv') as csvfile:
    lst = list(csv.reader(csvfile, delimiter=','))[1:]

# extract first character of each item in list as integer
ints = [int(i[0]) for i in lst]

# use collections.Counter for dictionary of counts
c = Counter(ints)

一种方法是使用
csv
模块读取文件,并使用中的配方创建一个(普通的)自定义
OrderedCounter
子类,以计算每个十进制数字是前导数字的次数

在下面的代码中,
OrderedCounter
初始化为所有可能数字的零计数,因此它们将出现在最终结果中_和u,以确定计数的存储顺序,并可以在输出中看到,如您所见

import csv
from collections import Counter, OrderedDict
from pprint import pprint

class OrderedCounter(Counter, OrderedDict):
    'Counter that remembers the order elements are first encountered'
    def __repr__(self):
        return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))

leading_digit_counter = OrderedCounter({d: 0 for d in range(10)})
csv_filename = 'the_values.csv'

with open(csv_filename, 'r', newline='') as csv_file:
    reader = csv.reader(csv_file)
    next(reader)  # Skip header row.
    leading = map(lambda x: int(x[0][0]), reader)  # Leading digit of each row.
    leading_digit_counter.update(leading)
    pprint(leading_digit_counter)
根据问题中的样本数据生成的输出:

{0:0,
1: 2,
2: 1,
3: 0,
4: 1,
5: 0,
6: 0,
7: 0,
8: 0,
9: 2}

到目前为止,您尝试了什么?发布您现有的脚本,我们将从那里开始工作。下面的解决方案之一有帮助吗?如果有,请随意接受(左边绿色勾选),或要求澄清。@Ngonix如果您觉得这有帮助,请选择作为最佳答案。