相关疑难解决方法(0)

在dthon中将dict转换为已排序的dict

我想在dthon中将dict转换为已排序的dict

data = pandas.read_csv('D:\myfile.csv')
for colname, dtype in data.dtypes.to_dict().iteritems():
    if dtype == 'object':
        print colname
        count = data[colname].value_counts()
        d = dict((str(k), int(v)) for k, v in count.iteritems())
        f = dict(sorted(d.iteritems(), key=lambda item: item[1], reverse = True)[:5])
        print f

        m ={}
        m["count"]= int(sum(count))    
        m["Top 5"]= f    
        print m    
        k = json.dumps(m)
        print k    
f = {'Gears of war 3': 6, 'Batman': 5, 'gears of war 3': 4, 'Rocksmith': 5, 'Madden': 3}
Run Code Online (Sandbox Code Playgroud)

我想要的输出是:

f = {'Gears of war 3': 6, 'Batman': 5, …
Run Code Online (Sandbox Code Playgroud)

python sorteddictionary pandas

9
推荐指数
1
解决办法
1万
查看次数

标签 统计

pandas ×1

python ×1

sorteddictionary ×1