python - 数据帧字典中列的总和

标签 python dataframe dictionary

我如何使用数据框字典?或者,是否有更好的方法来概览我的数据?例如,如果我有:

Fruit   Qty Year
Apple   2   2016
Orange  1   2017
Mango   2   2016
Apple   9   2016
Orange  8   2015
Mango   7   2016
Apple   6   2016
Orange  5   2017
Mango   4   2015

然后我试图找出我每年总共得到多少,例如:

        2015    2016    2017
Apple   0       11      0
Orange  8       0       6
Mango   4       9       0

我写了一些代码,但可能没有用:

import pandas as pd

# Fruit Data
df_1 = pd.DataFrame({'Fruit':['Apple','Orange','Mango','Apple','Orange','Mango','Apple','Orange','Mango'], 'Qty': [2,1,2,9,8,7,6,5,4], 'Year': [2016,2017,2016,2016,2015,2016,2016,2017,2015]})

# Create a list of Fruits
Fruits = df_1.Fruit.unique()

# Break down the dataframe by Year
df_2015 = df_1[df_1['Year'] == 2015]
df_2016 = df_1[df_1['Year'] == 2016]
df_2017 = df_1[df_1['Year'] == 2017]

# Create a dataframe dictionary of Fruits
Dict_2015 = {elem : pd.DataFrame for elem in Fruits}
Dict_2016 = {elem : pd.DataFrame for elem in Fruits}
Dict_2017 = {elem : pd.DataFrame for elem in Fruits}

# Store the Qty for each Fruit x each Year
for Fruit in Dict_2015.keys():
    Dict_2015[Fruit] = df_2015[:][df_2015.Fruit == Fruit]
for Fruit in Dict_2016.keys():
    Dict_2016[Fruit] = df_2016[:][df_2016.Fruit == Fruit]
for Fruit in Dict_2017.keys():
    Dict_2017[Fruit] = df_2017[:][df_2017.Fruit == Fruit]

最佳答案

您可以使用pandas.pivot_table .

res = df.pivot_table(index='Fruit', columns=['Year'], values='Qty',
                     aggfunc=np.sum, fill_value=0)

print(res)

Year    2015  2016  2017
Fruit                   
Apple      0    17     0
Mango      4     9     0
Orange     8     0     6

有关使用指南,请参阅 How to pivot a dataframe .

关于python - 数据帧字典中列的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50251956/

相关文章:

python - opencv+python+linux+webcam = 无法捕获帧

python - PyQt4 Widgets 的样式表组件名称

python - 计算由另一个列值分组的每个列的百分位值 - Pandas dataframe

.NET - 字典锁定与 ConcurrentDictionary

ios - 如何将模型类字典中的值存储在 Array/NSMutableArray 中?

c++ - 混合 C 和 C++、原始指针和( boost )共享指针

Python shlex - 拆分

python - R : install matplotlib in the new Rstudio preview version

r - 将字符串拆分为 2 个字符的组合并扩展为 R 中的数据框

python - 在 Spark 中更新数据框列