简体   繁体   English

如何使用 Pandas (Python) 对行进行分组并对列中的数字求和

[英]How to group Row and sum the number in Column using Pandas (Python)

Please help, I don't know how to code.请帮忙,我不知道如何编码。

I want to group the Column name 'Measure type' into 4 types:我想将列名“测量类型”分为 4 种类型:

  • Price价格
  • Volume体积
  • Protein蛋白质
  • Butterfat乳脂

And after that, I want to sum value in each column name: 'Price', 'Volume', 'Protein' and 'Butterfat'在那之后,我想在每个列名中求和值:'Price'、'Volume'、'Protein'和'Butterfat'

In the last step, I want to remove the NaN value or maybe can replace NaN with 0?, I don't know please help.在最后一步中,我想删除 NaN 值或者可以将 NaN 替换为 0?,我不知道请帮忙。

please open the example image in the URL below.请在下面的 URL 中打开示例图像。

Thank you谢谢

Example in URL URL 中的示例

If you want to programme something, you should start learning it.如果你想编程,你应该开始学习它。 This platform is made to help with certain problems.该平台旨在帮助解决某些问题。 Every helper wants to see that you have at least tried it before asking.每个帮助者都希望看到您在询问之前至少尝试过。 Then, but only then, you should ask.然后,但只有到那时,你才应该问。

Here's a little hint.这里有一个小提示。

import pandas as pd

l = [[1, 2, 3,], [1, None, 4], [2, 1, 3], [1, 2, 2]]

df = pd.DataFrame(l, columns=["a", "b", "c"])

df.groupby(by=["a"], dropna=True).sum()

Output: Output:

    b   c
a       
1   4.0 9
2   1.0 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM