Group brands


In [2]:
%matplotlib inline
import numpy as np
import pandas as pd
from scipy.stats import entropy
from tabulate import tabulate
from pymongo import MongoClient
import matplotlib.pyplot as plt
plt.style.use('seaborn')
plt.rcParams["figure.figsize"] = (20,8)


db = MongoClient()['stores']

TOTAL_NUMBER_OF_PRODUCTS = db.data.count()

results = db.data.aggregate(
    [
        {
            "$group": {
                "_id": "$brand",
                "count": {"$sum": 1},
            }
        },
        {
            "$sort": {
                "count": -1,
            }
        }
    ]
)

ALL_BRANDS = [(str(x['_id']), x['count']) for x in list(results)]
print('Number of uniq. brands: {}'.format(len(ALL_BRANDS)))


Number of uniq. brands: 338

In [3]:
DISTRIBUTORS = list(db.data.distinct("source"))
results = db.data.aggregate(
    [
        {
            "$group": {
                "_id": "$source",
                "brands": {"$addToSet": "$brand"},
            }
        },
        {
            "$project": {
                "_id": 1,
                "count": {"$size": "$brands"}
            }
        },
        {
            "$sort": {
                "count": -1,
            }
        }
    ]
)
BRANDS_PER_DISTRIBUTOR = [
    (str(x['_id']), x['count']) 
    for x in list(results)
]
print(tabulate(BRANDS_PER_DISTRIBUTOR, 
               headers=['Distributor', 'Number of uniq. Brands'], 
               tablefmt="simple"))

df_values_by_key = pd.DataFrame(BRANDS_PER_DISTRIBUTOR, 
                                index=[x[0] for x in BRANDS_PER_DISTRIBUTOR], 
                                columns=['Distributor', 'Brands'])
df_values_by_key.iloc[::-1].plot.barh()


Distributor      Number of uniq. Brands
-------------  ------------------------
marsha                              123
ignacia                             114
titus                                78
evie                                 73
larry                                72
jeneva                               66
patti                                58
kina                                 40
shemeka                              33
wendell                              31
major                                22
joanie                               16
carey                                15
rossana                              15
yolande                              12
rubi                                 10
ciara                                 1
allegra                               1
denisha                               1
zack                                  1
Out[3]:
<matplotlib.axes._subplots.AxesSubplot at 0x112bf4ac8>

Normalize brands


In [18]:
all_brands = list(map(lambda x: x.upper(), filter(None, results)))
NORMALIZED_BRANDS = []
for res in sorted(all_brands):
    if res not in NORMALIZED_BRANDS:
        NORMALIZED_BRANDS.append(res.strip())
    
print("Normalized brands w/o duplicated: {}".format(len(NORMALIZED_BRANDS)))
print(", ".join(NORMALIZED_BRANDS))


Normalized brands w/o duplicated: 271
24/7, 24/7 LIFESTYLE, A4, A4 DROP SHIP, ACRUX, ADAMS, ADAMS CAP, ADAMS CAPS, ADIDAS, ADIDAS BAGS, ADIDAS GOLF, ALL SPORT, ALO, ALO - ALL SPORT, ALO SPORT, ALPINE FLEECE, ALSTYLE, ALSTYLE FIRST QUALITY, ALSTYLE IRREGULAR, ALTERNATIVE, ALTERNATIVE APPAREL, AME & LULU, AMERICAN APPAREL, AMERICAN APPAREL DROP SHIP, ANVIL, APPLES & ORANGES, ASH CITY, ASH CITY - CORE 365, ASH CITY - EXTREME, ASH CITY - IL MIGLIORE, ASH CITY - NORTH END, ASH CITY - NORTH END SPORT BLUE, ASH CITY - NORTH END SPORT RED, ASH CITY NORTH END, ASHWORTH, AUGUSTA, AUGUSTA DROP SHIP, AUGUSTA SPORTSWEAR, AUTHENTIC, AUTHENTIC HEADWEAR, AUTHENTIC PIGMENT, BACKPACKER, BADGER, BADGER SPORT, BAGEDGE, BALI, BASECAMP, BAYSIDE, BELLA, BELLA + CANVAS, BELLA CANVAS, BELLA+CANVAS, BERMUDA SANDS, BIG ACCESSORIES, BLUE 84, BOXERCRAFT, BRIGHT SHIELD, BRIGHTLINE, BROWNING, BULWARK, BURNSIDE, C2, C2 SPORT, CALLAWAY, CALVIN KLEIN, CANVAS, CARMEL TOWEL COMPANY, CARMEL TOWELS, CAROLINA SEWN, CHAMPION, CHAMPION APPAREL, CHEF DESIGNS, CHESTNUT HILL, CHOUINARD, CODE FIVE, CODE FIVE DROP SHIP, CODE V, COLLEGIATE COTTON, COLORADO CLOTHING, COLORADO CLOTHING OUTERWEAR, COLORADO TRADING, COLORTONE, COLUMBIA, COMFORT COLORS, COMFORT COLORS DROP SHIP, CORE 365, CORNERSTONE, COTTON HERITAGE, COTTON PLUS, COTTON SWAG, CUBAVERA, DECORATION SUPPLIES, DELTA APPAREL, DEVON & JONES, DEVON AND JONES, DICKIES, DICKIES CHEF, DICKIES DROP SHIP, DICKIES MEDICAL, DICKIES OCCUPATIONAL, DISTRICT, DISTRICT CLOTHING, DISTRICT MADE, DODGER, DOGGIE SKINS, DOGGIE SKINS DROP SHIP, DRI DUCK, DRI-DUCK APPAREL, DUNBROOKE, DUOFOLD BY CHAMPION, DYENOMITE, DYENOMITE DROP SHIP, EAGLE USA, ECONSCIOUS, EDDIE BAUER, EI-LO, EXTREME, FAHRENHEIT, FEATHERLITE, FLEXFIT, FORTRESS, FRENCH TOAST, FRUIT OF THE LOOM, FUL, GEMLINE, GILDAN, GILDAN - HEAVY COTTON™, GILDAN - YOUTH, GILDAN FIRST QUALITY, GILDAN IRREGULAR, GILDAN – YOUTH, GREAT REPUBLIC, HALL OF FAME, HANES, HANES ULTIMATE, HARDWARE, HARRITON, HARVARD SQUARE, HEADSWEATS, HILTON, HOLLOWAY, HOOK & TACKLE, HTT, HYP, IGLOO, IN YOUR FACE, INDEPENDENT TRADING CO, INDEPENDENT TRADING CO., INNER HARBOR, IZOD, J AMERICA, J. AMERICA, JERZEES, JOMA SPORT, JONATHAN COREY, JUST MY SIZE, KARIBAN, KATI, KC CAPS, L A T, L'EGGS, L.A.T SPORTSWEAR, LA T, LAT, LAT DROP SHIP, LAT SPORTSWEAR, LEE, LIBERTY BAGS, LIBERTY BAGS DROP SHIP, LILYETTE, LIMITED LOGO APPAREL, LOS ANGELES APPAREL, LOWCOUNTRY, MAIDENFORM, MAJESTIC, MARMOT, MC, MEGA CAP, MEGACAP, MILLTEX, ML KISHIGO, MOSSY OAK, MV SPORT, NEW BALANCE, NEW ERA, NEXT LEVEL, NEXT LEVEL APPAREL, NEXT LEVEL RACK, NIKE, NORTH END, NYNE, OAD, OAKLEY, OCCUNOMIX, OGIO, OGIO ENDURANCE, ORIGINAL CHUCK, OURAY, OUTDOOR CAP, PARAGON, PERFECT POCKET, PLAYTEX, PORT & COMPANY, PORT AUTHORITY, PRECIOUS CARGO, PRIVATE LABEL FIRST QUALITY, PRIVATE LABEL IRREGULARS, PRO TOWELS, PROLINE, PUMA, Q-TEES, RABBIT SKINS, RABBIT SKINS DROP SHIP, RAWLINGS, RED HOUSE, RED KAP, REEBOK, RICHARDSON, RIVER'S END, ROBINSON APPAREL, RUSSELL ATHLETIC, RUSSELL OUTDOOR, RUSSELL OUTDOORS, SHERWOOD, SIERRA PACIFIC, SKINS GAME, SOFFE, SOYBU, SPORT TEK, SPORT-TEK, SPORTSMAN, SPORTSMAN CAP, SPORTSMAN CAPS, SPY, STORM CREEK, STORMTECH, SUBLIVE, SUBLIVIE, SUBLIVIE DROP SHIP, TEAM 365, TEAM SPORTSMAN, THE STADIUM CHAIR, THREADFAST APPAREL, THREE RIVERS / JERZEES, TIE DYE, TIE-DYE, TIE-DYE DROP SHIP, TIE-DYED, TOWELS PLUS, TOWELS PLUS BY ANVIL, TRECENTO, TULTEX, TULTEX COLLECTION, ULTRACLUB, ULTRACLUB BY CARMEL TOWEL, ULTRACLUB BY LIBERTY BAGS, UNION MADE, US BLANKS, VALEO, VALUBAG, VALUCAP, VAN HEUSEN, WALLS DROP SHIP, WEATHERPROOF, WHISPERING PINES, WILLOW POINTE, WRANGLER, WRANGLER WORKWEAR, YUPOONG, YUPOONG-FLEX FIT, ZKAPZ

In [ ]: