排列并查找列表中出现次数最多的项目

Posted

技术标签:

【中文标题】排列并查找列表中出现次数最多的项目【英文标题】:Arrange and find most appeared item in the list 【发布时间】:2020-04-19 13:18:17 【问题描述】:

Scott Logistics Corp
Transportation One LLC
Brothers Logistics Inc
Western Express Inc
Dart Advantage Logistics
Western Express Inc
Western Express Inc
Landstar Inway
Circle Logistics Inc
Rightway Logistics
Rightway Logistics
Rightway Logistics
Spike Freight Service
Rightway Logistics
Rightway Logistics
AMX Logistics
T A Services Inc Formerly Team America Inc
J & R Schugel/Super Service
NFI Logistics/NFI Transportation
Titanium Trucking Services/Titanium Logistics
Patterson Companies Inc
Arrive Logistics
Cavalry Logistics Llc
Landstar Ranger Inc
Landstar Ranger Inc
Logistic Dynamics Inc/Ldi Trucking Inc
US Xpress Inc
US Xpress Inc
XPO Logistics LLC
Bedrock Logistics
Transfix Inc
Convoy Inc
Choctaw Logistics Llc
Trekker Logistics LLC
Landstar Ranger Inc
MAG Carriers Llc/Mag Transportation Llc
Capital Logistics Group LLC/Clg Transportation
Capital Logistics Group LLC/Clg Transportation
Landstar Ranger Inc
XPO Logistics LLC

以上是公司名称的数据集,其中某些名称出现在相同情况下两个大于两个。我想要一个代码来安排它们。有的公司名称出现了不止一个,但在不同的位置,如何将它们排列到一个位置? 其实我想统计哪家公司出现在数据中的次数最多。如果有其他方法可以找出出现次数最多的公司,请指教

【问题讨论】:

Salam Zeeshan,您能展示一下您的示例输出吗?到目前为止您尝试了什么? 这能回答你的问题吗? Pandas count(distinct) equivalent #print All value of column for i in range(1, 1045, 1): list= (i, sheet.cell(row=i, column=9).value) def most_frequent(list): return max(set(list), key = list.count) print(most_frequent(list)) @Datanovice 不,我正在从 excel 文件中读取数据并使用 JupiterNotebook。 请使用您帖子下方的edit 链接使用您的代码更新它。 【参考方案1】:

你可以使用集合中的Counter函数(python中的核心模块之一),你不需要安装它:

from collections import Counter
counts = Counter(your_list)

【讨论】:

【参考方案2】:

您可以使用熊猫。

试试这个,

import pandas as pd

我为您提供的数据创建了一个文件,然后将其导入:

df = pd.read_csv('company.txt', header=0)

文件看起来像这样,

 name
"Scott Logistics Corp"
"Transportation One LLC"
"Brothers Logistics Inc"
"Western Express Inc"
"Dart Advantage Logistics"
"Western Express Inc"
"Western Express Inc"
"Landstar Inway"
"Circle Logistics Inc"
....

那么,

这样获取重复次数最多的名字,

print('**Name most repeated**')
print(df['name'].value_counts().idxmax())

获取该名称的重复次数。

print('**this many times**')
print(df['name'].value_counts().max())

对于您提供的数据,输出如下所示,

**Name most repeated**
Rightway Logistics
**this many times**
5

【讨论】:

import pandas as pd df = pd.read_excel('new_edata.xlsx', header = 0 ) print('**Name most repeated**') print(df['Northeast Logistics'].value_counts().idxmax()) 错误**Name most repeated** --------------------------------------------------------------------------- KeyError Traceback (most recent call last) ~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2656 try: -> 2657 return self._engine.get_loc(key) 2658 except KeyError: KeyError: 'Northeast Logistics' 当名称用双引号或单引号时有效。可以请分享如何在不影响行数的情况下将它们添加到引号中。谢谢 我将它们复制到文本编辑器(Gedit),但任何应该都可以工作,然后查找并替换。查找 (.+) 并替换 "/1" 确保您单击显示正则表达式的对话按钮。 我已经用 Notepad++ 试过了。那是用 "/1" 替换行。我不想替换名称,但想为每个名称添加单引号或双引号。谢谢

以上是关于排列并查找列表中出现次数最多的项目的主要内容,如果未能解决你的问题,请参考以下文章

如何查找在熊猫数据框中出现最多的行项目

JS中如何查找出现次数最多的字节

js查找数组中出现次数最多的元素

Js对象(五)如何查找字符串中出现次数最多的字符 2021-11-24

查找出现次数最多的数

学习 javascript 第一天:如何使用 javascript 查找字符串中出现次数最多的字符