如何在按另一列过滤时取一列的平均值';python中的标准
我正在使用Python(通常是一个R语言的人),我正在尝试为一个特定的应用程序创建这个函数。基本上,我试图在“年中的月”列中取每个月“CallsPresented”列的平均值。我知道我让事情变得比我需要的更复杂。我应该如何做到这一点如何在按另一列过滤时取一列的平均值';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',
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'?您可以发布示例数据或示例数据框吗?如果你包括一个答案,你会得到更好的答案。你在找这样的东西吗?梅怎么了?