小编Dav*_*ino的帖子

Python pandas groupby聚合在多个列上,然后是pivot

在Python中,我有一个类似于以下的pandas DataFrame:

Item | shop1 | shop2 | shop3 | Category
------------------------------------
Shoes| 45    | 50    | 53    | Clothes
TV   | 200   | 300   | 250   | Technology
Book | 20    | 17    | 21    | Books
phone| 300   | 350   | 400   | Technology
Run Code Online (Sandbox Code Playgroud)

shop1,shop2和shop3是不同商店中每件商品的成本.现在,我需要在一些数据清理后返回一个DataFrame,如下所示:

Category (index)| size| sum| mean | std
----------------------------------------
Run Code Online (Sandbox Code Playgroud)

其中size是每个Category中的项目数和sum,mean和std与应用于3个商店的相同功能相关.如何使用split-apply-combine模式(groupby,aggregate,apply,...)执行这些操作?

有人可以帮我吗?我对这个疯狂了......谢谢!

python pivot dataframe pandas data-cleaning

23
推荐指数
2
解决办法
5万
查看次数

鼠标单击事件后刷新绘图

我需要在单击鼠标后刷新 matplotlib 条形图。该图应采用 event.ydata 值并根据它重新绘制数据。我能够从鼠标事件中检索该值,但是当我尝试刷新绘图时似乎没有发生任何事情。这是我的代码:

#df is a pd.DataFrame, I have to plot just the df_mean with error bars (marginOfError)
df_mean = df.mean(axis=1)
df_std = df.std(axis=1)
marginOfError = 1.96*df_std/np.sqrt(3650)

index = np.arange(4)

def print_data(threshold):    
    colors = []

    for i,err in zip(df_mean,marginOfError):
        if(i + err < threshold):
            colors.append('#001f7c')
        elif(i - err > threshold):
            colors.append('#bc0917')
        else:
            colors.append('#e0d5d6')

    fig, ax = plt.subplots()

    plt.bar(index, df_mean, 0.85,
                     alpha=0.85,
                     color=colors,
                     yerr=marginOfError)

    plt.xticks(index, df.index)

    #horizontal threshold line
    ax.plot([-0.5, 3.5], [threshold, threshold], "c-")

# first print data with …
Run Code Online (Sandbox Code Playgroud)

python matplotlib event-handling bar-chart

3
推荐指数
1
解决办法
2320
查看次数