如何计算每月的平均价值?

时间:2018-06-15 18:57:00

标签: python pandas matplotlib

我有以下功能:

def plot_distribution(df, var, target, **kwargs):
    row = kwargs.get('row', None)
    col = kwargs.get('col', None)
    facet = sns.FacetGrid(df, hue=target, aspect=4, row = row, col = col)
    facet.map(sns.kdeplot, var, shade=True)
    facet.set(xlim=(0, df[var].max()))
    facet.add_legend()

plot_distribution(asma_df, var = 'ADDITIONAL_ASMA_40', target = 'RUNWAY', row = 'RUNWAY')

此功能可创建以下图表:

enter image description here

我希望以这样的方式更改它:X轴包含月平均值,而Y轴包含每月ADDITIONAL_ASMA_40的平均值。

这是一个示例DataFrame df

month    ADDITIONAL_ASMA_40    RUNWAY
1        20                    32L
1        22                    32L
1        18                    32R
2        25                    32L
2        26                    32L
2        25                    32L
2        25                    32R

1 个答案:

答案 0 :(得分:0)

只需使用groupby功能

df.groupby('month').mean()

并绘制其感兴趣的列