简体   繁体   English

Python 通过嵌套字典排序

[英]Python Sorting through nested dictionary

From multiple tables I'm getting values of the form: (Sector, Stock, InvestedValue).从多个表中,我得到以下形式的值:(Sector,Stock,InvestedValue)。

I'm using a Python dictionary object to insert these values and during insert if the combination (sector, stock) exists add the InvestedValue to the existing entry else add new entry to dictionary.我正在使用 Python 字典 object 来插入这些值,如果存在组合(部门、股票),则在插入期间将 InvestedValue 添加到现有条目中,否则将新条目添加到字典中。 After all data insert, let's say I end up with nested dictionary like:在所有数据插入之后,假设我最终得到了嵌套字典,例如:

stock_dict = {
    "Financial": {"HDFC Bank": 230.25, "Axis Bank": 70.15, "ICICI Bank": 110.45},
    "Automobile": {"Tata Motors": 135.67},
    "Consumer Goods": {"Avenue Supermarket": 190.45, "Godrej Industries": 120.32}
}

How to print through this nested dictionary in sorted fashion:如何以排序方式打印此嵌套字典:

  1. Get combination of Sector,Company,InvestedValue sorted on the basis of InvestedValue获取基于 InvestedValue 排序的 Sector、Company、InvestedValue 组合

  2. Get combination of Sector, sum(InvestedValue) ie.获取 Sector 和 sum(InvestedValue) 的组合,即。 sum of InvestedValue of each company in that sector again sorted on the sum该行业中每家公司的 InvestedValue 总和再次按总和排序

My current approach to solve these problems is to flatten the nested dictionary to list of tuples and run sorted on it.我目前解决这些问题的方法是将嵌套字典展平为元组列表并在其上运行排序。 For example:例如:

To solve 1:解决1:

stock_list = []
for sector in stock_dict:
    for stock in stock_dict[sector]:
        stock_list.append((sector, stock, stock_dict[sector][stock]))
sorted_list = sorted(stock_list, key=lambda stock: stock[2], reverse=True)

To solve 2:解决2:

sector_list = []
for sector in stock_dict:
    sector_list.append((sector,sum(stock_dict[sector].values())))
sorted_sector_list = sorted(sector_list, key=lambda sector: sector[1], reverse=True)

IS there a better approach ie sort directly on the nested dictionary without having to flatten it into the list?有没有更好的方法,即直接在嵌套字典上排序而不必将其展平到列表中?

If you uses pandas, it can convert you dict to a dataframe.如果您使用 pandas,它可以将您的 dict 转换为 dataframe。 You can then unstack the dataframe which will make it long form instead of wide.然后,您可以将 dataframe 拆开,这将使其长形而不是宽形。 Once it's in the long format, you will have null values for any sector which a company does not participate in, you can drop those with dropna(), reset the index and sort values by your desired column.一旦它采用长格式,您将获得公司不参与的任何部门的 null 值,您可以使用 dropna() 删除这些值,重置索引并按所需列对值进行排序。 Once this is done rename your columns to the desired names, and select them in the order you want.完成此操作后,将列重命名为所需的名称,并按您想要的顺序将它们重命名为 select。 You can take that same df and groupby sector and sum the InvestedValue.您可以采用相同的 df 和 groupby 扇区并对 InvestedValue 求和。

import pandas as pd
stock_dict = {
    "Financial": {"HDFC Bank": 230.25, "Axis Bank": 70.15, "ICICI Bank": 110.45},
    "Automobile": {"Tata Motors": 135.67},
    "Consumer Goods": {"Avenue Supermarket": 190.45, "Godrej Industries": 120.32}
}

df = pd.DataFrame.from_dict(stock_dict, orient='index')
df = df.unstack().dropna().reset_index(name='InvestedValue').sort_values(by='InvestedValue', ascending=False)

df.columns = ['Company','Sector','InvestedValue']
df[['Sector','Company','InvestedValue']]

Output Output

           Sector             Company  InvestedValue
0       Financial           HDFC Bank         230.25
4  Consumer Goods  Avenue Supermarket         190.45
3      Automobile         Tata Motors         135.67
5  Consumer Goods   Godrej Industries         120.32
2       Financial          ICICI Bank         110.45
1       Financial           Axis Bank          70.15

2nd Part第二部分

df.groupby('Sector')['InvestedValue'].sum().reset_index().sort_values(by='InvestedValue', ascending=False)

Output Output

           Sector  InvestedValue
2       Financial         410.85
1  Consumer Goods         310.77
0      Automobile         135.67

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM