Pandas Dataframe groupby语句输出到2列

2024-05-15 20:56:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一本价值观词典:

{'Spanish Omlette': -0.20000000000000284,
 'Crumbed Chicken Salad': -1.2999999999999972,
 'Chocolate Bomb': 0.0,
 'Seed Nut Muesli': -3.8999999999999915,
 'Fruit': -1.2999999999999972,
 'Frikerdels Salad': -1.2000000000000028,
 'Seed Nut Cheese Biscuits': 0.4000000000000057,
 'Chorizo Pasta': -2.0,
 'No carbs Ice Cream': 0.4000000000000057,
 'Veg Stew': 0.4000000000000057,
 'Bulgar spinach Salad': 0.10000000000000853,
 'Mango Cheese': 0.10000000000000853,
 'Crumbed Calamari chips': 0.10000000000000853,
 'Slaw Salad': 0.20000000000000284,
 'Mango': -1.2000000000000028,
 'Rice & Fish': 0.20000000000000284,
 'Almonds Cheese': -0.09999999999999432,
 'Nectarine': -1.7000000000000028,
 'Banana Cheese': 0.7000000000000028,
 'Mediteranean Salad': 0.7000000000000028,
 'Almonds': -4.099999999999994}

我试图用熊猫从字典中得出每种食物的价值总和:

^{pr2}$

上述代码给出正确的输出:

^{3}$

但是,我需要得到2列的输出,而不是上面熊猫给我的。在

如何将输出转换成可以打印数据的格式。一、 E标签和值作为单独的值


Tags: 词典seed价值观chickennutcheesespanishmango
3条回答

分组后,您需要重置索引或在调用groupby时使用as_index=False。按照post的说法,默认情况下,聚合函数将不会返回正在聚合的组(如果它们是命名列)。相反,分组列将是返回对象的索引。传递as_index=False或随后调用reset_index将返回正在聚合的组(如果它们是命名列)。在

请看下面我试图将您的结果转换成有意义的图表:

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
df = fooddata.reset_index()
ax = df[['food','sum']].plot(kind='barh', title ="Total Sum per Food Item", figsize=(15, 10), legend=True, fontsize=12)
ax.set_xlabel("Sum per Food Item", fontsize=12)
ax.set_ylabel("Food Items", fontsize=12)
ax.set_yticklabels(df['food'])
plt.show()

结果是this

您可以在^{}和聚合^{}中使用参数as_index=False

fooddata = pd.DataFrame(list(foodWeight.items()), columns=['food','weight'])

print (fooddata.groupby('food', as_index=False)['weight']
               .sum()
               .sort_values(by='weight', ascending=0))
                        food  weight
2              Banana Cheese     0.7
12        Mediteranean Salad     0.7
20                  Veg Stew     0.4
14        No carbs Ice Cream     0.4
16  Seed Nut Cheese Biscuits     0.4
18                Slaw Salad     0.2
15               Rice & Fish     0.2
3       Bulgar spinach Salad     0.1
6     Crumbed Calamari chips     0.1
11              Mango Cheese     0.1
4             Chocolate Bomb     0.0
1             Almonds Cheese    -0.1
19           Spanish Omlette    -0.2
10                     Mango    -1.2
8           Frikerdels Salad    -1.2
9                      Fruit    -1.3
7      Crumbed Chicken Salad    -1.3
13                 Nectarine    -1.7
5              Chorizo Pasta    -2.0
17           Seed Nut Muesli    -3.9
0                    Almonds    -4.1

另一个解决方案是添加^{}

^{pr2}$

对于绘图,最好不要重置索引-然后索引值创建x轴-使用^{}

fooddata.groupby('food')['weight'].sum().sort_values(ascending=0).plot()

graph

或者如果需要绘图^{}

fooddata.groupby('food')['weight'].sum().sort_values(ascending=0).plot.barh()

graph

调用group by时设置为_index=False

fooddata = pd.DataFrame(list(foodWeight.items()), columns=['food','weight']).groupby('food',as_index=False).agg({"weight":"sum"}).sort_values(by='weight', ascending=0)

相关问题 更多 >