【问题标题】:How to order data in a dictionary in Python using Pandas如何使用 Pandas 在 Python 中对字典中的数据进行排序
【发布时间】:2019-09-28 21:42:05
【问题描述】:

我正在尝试按犯罪者的年龄以升序对我的输出进行排序。目前,它是完全无序的。

我尝试使用排序功能,但它不起作用。

    xl = pd.ExcelFile('Murders.xlsx')
    df = xl.parse('Sheet1')
    age = df['Perpetrator Age']

    freq1 = collections.Counter(df['Perpetrator Age'])
    freq = [{'Perpetrator_Age': m, 'Freq': f} for m, f in freq1.items()]
    file = open("MurderPerpAge.js", "w+")
    file.write(json.dumps(freq))
    file.close()

我希望我的输出按年龄从小到大排序。

[{"Perpetrator_Age": 15, "Freq": 5441}, {"Perpetrator_Age": 17, "Freq": 14196},...

【问题讨论】:

    标签: python-3.x pandas dictionary


    【解决方案1】:

    选项 1:纯 python

    用密钥试试sorted

    sorted(freq , key=lambda x: x["Perpetrator_Age"])
    

    选项 2:混合 Pandas 和纯 python

    freq1 = Counter(df['Perpetrator Age'].sort_values())
    freq = [{'Perpetrator_Age': m, 'Freq':f} for m,f in freq1.items()]
    

    选项 3:纯熊猫

    受 WeNYoBen 的回答启发。

    freq1 = df.groupby('Perpetrator Age').size()
    freq1.name = 'Freq'
    freq = freq1.reset_index().to_dict('r')
    

    【讨论】:

    • 我收到一个错误 TypeError: '
    • @treatyoself 关于哪个解决方案?解决方案 3 中出现错误,其中 groupby('Perpetrator_Age') 应为 groupby('Perpetrator Age')
    • 抱歉,忘记指定了。对于选项 2,我得到了错误。
    • 我的玩具数据集 df = pd.DataFrame({"Perpetrator Age": np.random.randint(15,50,1000)}) 运行良好。你能检查你的df['Perpetrator Age'] 是否包含字符串值吗?
    • 我正在读取一个 xlsx 文件。我过滤了数据,确实找到了一些“空白”值。会不会是这个问题?
    【解决方案2】:

    既然你提到了pandas,我使用的是value_counts,因为默认是按频率排序

    df['Perpetrator Age'].value_counts().reset_index().to_dict('r')
    

    【讨论】:

    • 好一个。我也在看to_dict,从你对to_dict('r')的回答中学到了新东西。
    猜你喜欢
    • 2014-11-10
    • 2018-05-08
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 1970-01-01
    • 2014-03-13
    • 2011-06-06
    相关资源
    最近更新 更多