Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在按另一列过滤时取一列的平均值';python中的标准_Python_Python 3.x_Pandas_Numpy_Anaconda - Fatal编程技术网

如何在按另一列过滤时取一列的平均值';python中的标准

如何在按另一列过滤时取一列的平均值';python中的标准,python,python-3.x,pandas,numpy,anaconda,Python,Python 3.x,Pandas,Numpy,Anaconda,我正在使用Python(通常是一个R语言的人),我正在尝试为一个特定的应用程序创建这个函数。基本上,我试图在“年中的月”列中取每个月“CallsPresented”列的平均值。我知道我让事情变得比我需要的更复杂。我应该如何做到这一点 def get_monthly_mean(df): avg_by_month = [] months = ['Jan', 'Feb', 'Mar', 'Apr', 'June', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov',

我正在使用Python(通常是一个R语言的人),我正在尝试为一个特定的应用程序创建这个函数。基本上,我试图在“年中的月”列中取每个月“CallsPresented”列的平均值。我知道我让事情变得比我需要的更复杂。我应该如何做到这一点

def get_monthly_mean(df):
    avg_by_month = []
    months = ['Jan', 'Feb', 'Mar', 'Apr', 'June', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    for i in range(11):
        x = np.mean(df['CallsPresented'][df.loc[df['Month_of_Year'] == months[i]]])
        avg_by_month.append(x)
    return months

为什么不直接按
groupBy
month列计算每个组的
mean

差不多

def get_monthly_mean(df):

    df_grouped = df.groupby('Month_of_Year')['CallsPresented'].mean()

    #Then you can pass the column to a list or just return the grouped df, 
    #whatever suits your use case better

    return df_grouped

你在用numpy;请加上那个标签。这会帮助你找到专家。@wizzwizz4嗯,这更像是一件熊猫的事情。通常熊猫不带np标签。而且June'?您可以发布示例数据或示例数据框吗?如果你包括一个答案,你会得到更好的答案。你在找这样的东西吗?梅怎么了?