0

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

Above is the data set of the companies name where some name have appears ones in same case two are more than two. i want a code to arrange them up. Some company names have appears more then one but are at different location, How to arrange them to one location? actually i want to count which company appears the most in the data. if there is any other way to find out which name appears the most please advise

Zeeshan
  • 11
  • 5
  • 1
    Salam Zeeshan, can you show your sample output and what have you tried so far? – Umar.H Dec 30 '19 at 21:11
  • Does this answer your question? [Pandas count(distinct) equivalent](https://stackoverflow.com/questions/15411158/pandas-countdistinct-equivalent) – Umar.H Dec 30 '19 at 21:14
  • `#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)) ` – Zeeshan Dec 30 '19 at 21:18
  • @Datanovice No, i am reading data from excel file and working on JupiterNotebook. – Zeeshan Dec 30 '19 at 21:22
  • Please use the [edit] link under your post to update it with your code. – Gino Mempin Dec 31 '19 at 02:00

2 Answers2

2

You can use Counter function from collections (one of core modules in python), you don't need to install it:

from collections import Counter
counts = Counter(your_list)
YOLO
  • 20,181
  • 5
  • 20
  • 40
1

You can use Pandas.

try this,

import pandas as pd

I created a file for the data you provided and then imported it:

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

file looks like this,

 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"
....

then,

Get the most repeated name like this,

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

get the number of times that name was repeated.

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

For the data you provided the output looks like this,

**Name most repeated**
Rightway Logistics
**this many times**
5
merit_2
  • 461
  • 5
  • 16
  • `import pandas as pd df = pd.read_excel('new_edata.xlsx', header = 0 ) print('**Name most repeated**') print(df['Northeast Logistics'].value_counts().idxmax())` – Zeeshan Dec 31 '19 at 10:46
  • ERROR `**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' ` – Zeeshan Dec 31 '19 at 10:48
  • it works when names are in double or single quotes. can please share how to add them in quotes without disturbing the number of row. thanks – Zeeshan Dec 31 '19 at 11:06
  • I copied them to a text editor (Gedit) but any should work, then find and replace. Find (.+) and Replace "/1" make sure you click the dialogue button that says regular expression. – merit_2 Dec 31 '19 at 16:43
  • I have tried it with Notepad++. that was replacing the rows with "/1" . I don't want to replace names but want to add single or double quotes to each name. thanks – Zeeshan Jan 01 '20 at 14:02