pandas / DataFrame的内置函数/方法可将重复的groupby值转换为矩阵?

时间:2019-03-16 08:09:24

标签: python pandas dataframe

我有一个数据框,可以对每个城市的每个价格范围的商品进行分组和计数,如下所示:

list_cities = ['city-1', 'city-2', 'city-3']
list_price_ranges = ['Budget', 'Standard', 'Luxury']
df = pd.DataFrame([['city-1','Budget',72], ['city-1','Standard',219], ['city-1','Luxury',73],
                   ['city-2','Budget',58], ['city-2','Standard',196], ['city-2','Luxury',75],
                   ['city-3','Budget',49], ['city-3','Standard',160], ['city-3','Luxury',46]],
                  columns=['city', 'price range', 'number of items'])
df

它导致:

    city    price range number of items
0   city-1  Budget       72
1   city-1  Standard    219
2   city-1  Luxury       73
3   city-2  Budget       58
4   city-2  Standard    196
5   city-2  Luxury       75
6   city-3  Budget       49
7   city-3  Standard    160
8   city-3  Luxury       46

现在,我想将上表转换为:

        Budget  Standard    Luxury
city-1   72       219        73
city-2   58       196        75
city-3   49       160        46

我可以使用如下代码行做到这一点:

dict_records = df.to_dict(orient='records')
list_cities_price_ranges = []
for city in list_cities:
    list_city_price_ranges = []
    for price_range in list_price_ranges:
        for element in dict_records:
            if element['city'] == city and element['price range'] == price_range:
                list_city_price_ranges.append(element['number of items'])
    list_cities_price_ranges.append(list_city_price_ranges)

list_cities_price_ranges
df_cities_price_ranges = pd.DataFrame(list_cities_price_ranges, index=list_cities, columns=list_price_ranges)
df_cities_price_ranges

它工作正常。

但是,是否有任何内置的pandas / DataFrame函数/方法使代码更简单,更美观?

谢谢

0 个答案:

没有答案