admin管理员组文章数量:1023230
I have a dataframe in Python like this and I need to build a pivot table that as aggfunc first calculates the mean of each column for each label and then sum all the mean values by column.
Dear community, any idea about how I could do that? I was thinking at 2 sequential pivot tables but this doesn't sound efficient. thanks !
the result should be something like:
I have a dataframe in Python like this and I need to build a pivot table that as aggfunc first calculates the mean of each column for each label and then sum all the mean values by column.
Dear community, any idea about how I could do that? I was thinking at 2 sequential pivot tables but this doesn't sound efficient. thanks !
the result should be something like:
Share Improve this question asked Nov 18, 2024 at 19:51 Nic_barNic_bar 313 bronze badges 01 Answer
Reset to default 1You shouldn't put data as an image. But here is the code you should use,
df = pd.DataFrame(data)
# Calculate the mean for each label
mean_df = df.groupby("label").mean()
# Sum the mean values across the columns
totals = mean_df.sum()
# Display the result
result = pd.DataFrame([totals], index=["tot (mean a + mean b)"])
print(result)
I have a dataframe in Python like this and I need to build a pivot table that as aggfunc first calculates the mean of each column for each label and then sum all the mean values by column.
Dear community, any idea about how I could do that? I was thinking at 2 sequential pivot tables but this doesn't sound efficient. thanks !
the result should be something like:
I have a dataframe in Python like this and I need to build a pivot table that as aggfunc first calculates the mean of each column for each label and then sum all the mean values by column.
Dear community, any idea about how I could do that? I was thinking at 2 sequential pivot tables but this doesn't sound efficient. thanks !
the result should be something like:
Share Improve this question asked Nov 18, 2024 at 19:51 Nic_barNic_bar 313 bronze badges 01 Answer
Reset to default 1You shouldn't put data as an image. But here is the code you should use,
df = pd.DataFrame(data)
# Calculate the mean for each label
mean_df = df.groupby("label").mean()
# Sum the mean values across the columns
totals = mean_df.sum()
# Display the result
result = pd.DataFrame([totals], index=["tot (mean a + mean b)"])
print(result)
版权声明:本文标题:aggfunc where I calculate mean by index first and then sum by column in python pivot table - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745597619a2158263.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论