[英]How to group together rows of Pandas Dataframe with same values in first 2 columns by summing values in the 3rd column?
I have a dataframe of the form:我有一个 dataframe 的形式:
For same values of col1 and col2 (for example, AB), I want to add all values in the col3 of the dataframe such that only one row of that form (AB) remains and all values in col3 correspond to those are added.对于 col1 和 col2 的相同值(例如 AB),我想在 dataframe 的 col3 中添加所有值,以便只保留该表单 (AB) 的一行,并且 col3 中的所有值对应于添加的值。
The result would look like:结果将如下所示:
I tried:我试过了:
df.groupby(['col1', 'col2'], axis=0, as_index=True).sum()
but it gave me:但它给了我:
which is not exactly what I am looking for.这不正是我要找的。 Please help and advise.请帮助和建议。 Thanks in advance.提前致谢。
You just need to do this, False
instead of True
:你只需要这样做, False
而不是True
:
df.groupby(['col1', 'col2'], axis=0, as_index=False).sum()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.