Search code examples
pythonpandasdataframecluster-analysisk-means

how to map string value of dataframe to some number to plot Cluster?


I want to plot cluster of my data-set by the only method i know is to map string to some integer value. Like

data_mapped=data.copy()
data_mapped['Language']=data_mapped['Language'].map({'English':0,'French':1,'German':2})
data_mapped

But in this example i have only 3 unique value of Language that i can map by this method

Now I don't know how to covert multiple unique string values to integer values and plot cluster? I want to cluster by some column like (Color,fabric,dress_type) and I want to cluster whole data?

Now I have Data-set is:

file_list=glob.glob('json_file/[!Merg_all]*json')
merg_all_list=[]
for file in file_list:
    print(file)
    raw_data=pd.read_json(str(file))
    raw_data.head()
    for i in raw_data['product']:
        merg_all_list.append(i)

json file:

[{"product": {"brand_name": "So Kamal", "designer": "So Kamal", "title": "So Kamal Women Summer Collection Mustard Lawn 1PC -Unstitched Shirt DPL19 49 LA00964-Std-MST", "description": "description   specifications of so kamal women summer collection mustard lawn 1pc  unstitched shirt dpl19 49 la00964 std mst brand so kamal sku 105972128_pk 1253666066 features 1pc  unstitched main material lawn season summer material family lawn what's in the box 1x 1pc unstitched suit", "dress_type": "shirt", "where_to_wear": "", "color": "mustard", "stitched": false, "season": "summer", "price": 1120, "currency": "Rs", "product_id": "So Kamal Women Summer Collection Mustard Lawn 1PC -Unstitched Shirt DPL19 49 LA00964-Std-MST", "collection_url": "https://lawncollection.pk/brands/", "source": "https://lawncollection.pk/so-kamal-women-summer-collection-mustard-lawn-1pc-unstitched-shirt-dpl19-49-la00964-std-mst.html", "fabric": "lawn", "gender": "women", "frontpic": "https://lawncollection.pk/public/images/products//2019/04/so-kamal-women-summer-collection-mustard-lawn-1pc-unstitched-shirt-dpl19-49-la00964-std-mst-image1.jpeg", "backpic": "https://lawncollection.pk/public/images/products//2019/04/so-kamal-women-summer-collection-mustard-lawn-1pc-unstitched-shirt-dpl19-49-la00964-std-mst-image2.jpeg", "otherpics": ["https://lawncollection.pk/public/images/products//2019/04/so-kamal-women-summer-collection-mustard-lawn-1pc-unstitched-shirt-dpl19-49-la00964-std-mst-image1.jpeg", "https://lawncollection.pk/public/images/products//2019/04/so-kamal-women-summer-collection-mustard-lawn-1pc-unstitched-shirt-dpl19-49-la00964-std-mst-image2.jpeg"], "sku": "SKU: 105972128_PK-1253666066", "details": "https://lawncollection.pk/so-kamal-women-summer-collection-mustard-lawn-1pc-unstitched-shirt-dpl19-49-la00964-std-mst.html https:  lawncollection.pk so kamal women summer collection mustard lawn 1pc unstitched shirt dpl19 49 la00964 std mst.html so kamal so kamal women summer collection mustard lawn 1pc -unstitched shirt dpl19 49 la00964-std-mst description   specifications of so kamal women summer collection mustard lawn 1pc  unstitched shirt dpl19 49 la00964 std mst brand so kamal sku 105972128_pk 1253666066 features 1pc  unstitched main material lawn season summer material family lawn what's in the box 1x 1pc unstitched suit", "Category1_list": "unstitched", "size": {"xs": false, "s": false, "m": false, "xl": false, "xxl": false}}}]

Data-frame

    brand_name  designer    title   description dress_type  where_to_wear   color   stitched    season  price   ... source  fabric  gender  frontpic    backpic otherpics   details Category1_list  size    sku
0   Polo Ralph Lauren   Polo Ralph Lauren   Long Sleeve Knit Magic Fleece Sweatshirt    - Casual graphic print sweatshirt- Crew neckli...   sweatshirt      black   True        8544    ... https://www.zalora.com.ph/polo-ralph-lauren-lo...   cotton  man static.ph.zalora.net/p/polo-ralph-lauren-3175-...   static.ph.zalora.net/p/polo-ralph-lauren-3175-...   [static.ph.zalora.net/p/polo-ralph-lauren-3175...   https://www.zalora.com.ph/polo-ralph-lauren-lo...       {'xs': False, 's': False, 'm': False, 'xl': Fa...   NaN
1   Polo Ralph Lauren   Polo Ralph Lauren   Basic Mesh Polo Shirt   - Colour block polo shirt with brand print- Un...   shirt       red True        9265    ... https://www.zalora.com.ph/polo-ralph-lauren-ba...   cotton  man static.ph.zalora.net/p/polo-ralph-lauren-7554-...   static.ph.zalora.net/p/polo-ralph-lauren-7555-...   [static.ph.zalora.net/p/polo-ralph-lauren-7554...   https://www.zalora.com.ph/polo-ralph-lauren-ba...       {'xs': False, 's': False, 'm': False, 'xl': Fa...   NaN
2   MANGO Man   MANGO Man   Faux Shearling Denim Jacket - Denim jacket with wash detail- Collar neckli...   jacket      blue    True        4995    ... https://www.zalora.com.ph/mango-man-faux-shear...   denim   man static.ph.zalora.net/p/mango-man-9782-7201341-...   static.ph.zalora.net/p/mango-man-9783-7201341-...   [static.ph.zalora.net/p/mango-man-9782-7201341...   https://www.zalora.com.ph/mango-man-faux-shear...       {'xs': False, 's': False, 'm': False, 'xl': Fa...   NaN
3   Polo Ralph Lauren   Polo Ralph Lauren   Knit Magic Fleece Hoodie    - Embroidered front hoodie- Unlined- Hooded ne...               True        10598   ... https://www.zalora.com.ph/polo-ralph-lauren-kn...   cotton  man static.ph.zalora.net/p/polo-ralph-lauren-2320-...   static.ph.zalora.net/p/polo-ralph-lauren-2320-...   [static.ph.zalora.net/p/polo-ralph-lauren-2320...   https://www.zalora.com.ph/polo-ralph-lauren-kn...       {'xs': False, 's': True, 'm': True, 'xl': True...   NaN
4   MANGO Man   MANGO Man   Turtleneck Flecked Sweater  - Solid hue speckle-knit sweatshirt- High neck...   sweatshirt      brown   True        2995    ... https://www.zalora.com.ph/mango-man-turtleneck...   cotton  man static.ph.zalora.net/p/mango-man-1900-5990341-...   static.ph.zalora.net/p/mango-man-1900-5990341-...   [static.ph.zalora.net/p/mango-man-1900-5990341...   https://www.zalora.com.ph/mango-man-turtleneck...       {'xs': False, 's': False, 'm': False, 'xl': Fa...   NaN

Solution

  • Choose the appropriate visualization technique for your data.

    On categoricial data, bar charts are more appropriate than scatter plots, since you don't need the x axis to be numeric.

    Also choose appropriate algorithms... K-means only makes sense on continuous variables. Encoding categories as integers for k-means is just wrong. In your case, k-means would assume the average of English and German is exactly French.