Home > Software engineering >  How can I sort the list in this order by popularity (most number of times)?
How can I sort the list in this order by popularity (most number of times)?

Time:12-07

So I have a csv file which contains several data like this

1,8dac2b,ewmzr,jewelry,phone0,9759243157894736,us,69.166.231.58,vasstdc27m7nks3
2,668d39,aeqok,furniture,phone1,9759243157894736,jp,50.201.125.84,jmqlhflrzwuay9c
3,622r49,arqek,vehicle,phone2,9759544365415694736,az,53.001.135.54,weqlhrerreuert6f
4,6444t43,rrdwk,vehicle,phone9,9759543263245434353,au,54.241.234.64,weqqyqtqwrtert6f

and I'm tryna use this this function def popvote(list) to return the most popular thing in auction which in the example is vehicle

so I want my function to return what's the most popular thing in the 4th row.. which is vehicle in this case

This is what I have so far

def popvote(list):
    for x in list:
        g = list(x)
    return list.sort(g.sort)

However, this doesn't really work.. what should I change to make sure this works??

Note: The answer should be returned as a set

Edit: so I'm trying to return the value that is repeated most in the list based on what's indicated in (** xxxx **) below

1,8dac2b,ewmzr,**jewelry**,phone0,9759243157894736,us,69.166.231.58,vasstdc27m7nks3
    2,668d39,aeqok,**furniture**,phone1,9759243157894736,jp,50.201.125.84,jmqlhflrzwuay9c
    3,622r49,arqek,**vehicle**,phone2,9759544365415694736,az,53.001.135.54,weqlhrerreuert6f
    4,6444t43,rrdwk,**vehicle**,phone9,9759543263245434353,au,54.241.234.64,weqqyqtqwrtert6f

So in this case, vehicle should be the output.

CodePudding user response:

import pandas as pd
df = pd.read_csv("filename.csv")
most_common = df[df.columns[3]].value_counts().idxmax()

Any questions? Down in the comments.

CodePudding user response:

An alternative solution could be (assuming you have your records as list of lists):

from statistics import mode

mode(list(zip(*your_csv))[3]) # item type is listed as 4th argument
  • Related