使用python计算每个地址在数据文件中出现的次数

2024-05-04 11:17:34 发布

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


Tags: python
2条回答

你可以用^{}:

from collections import Counter 

words = []

for line in open('data.txt'):
  # Your logic here
  words.append(line.split()[1])

words_dict = Counter(words)

for key, value in words_dict.items():
  print(key, value)

输出:

303574 1
55596 3
303630 1
303567 1
303595 1
303616 1
240840 3
303588 1
55590 1
303623 1
303602 1
303581 1
55608 3
303609 1

IIUC,你可以这样对待熊猫:

import pandas as pd

df = pd.read_csv('Data_File_Path', sep='\s+', header=None, usecols=[1])
df[ df[1] < 6000000 ][1].value_counts()

输出:

55608     3
55602     3
55596     3
240840    3
303581    1
303609    1
303574    1
303567    1
303630    1
303595    1
303623    1
55590     1
303588    1
303602    1
303616    1
Name: 1, dtype: int64

相关问题 更多 >