python - 总结 Pandas DataFrame 中的列值

标签 python python-2.7 pandas

在 pandas DataFrame 中,是否可以折叠具有相同值的列,然后将另一列中的值相加?

代码

data = {"score":{"0":9.397,"1":9.397,"2":9.397995,"3":9.397996,"4":9.3999},"type":{"0":"advanced","1":"advanced","2":"advanced","3":"newbie","4":"expert"},"count":{"0":394.18930604,"1":143.14226729,"2":9.64172783,"3":0.1,"4":19.65413734}}
df = pd.DataFrame(data)
df

输出

     count       score       type
0    394.189306  9.397000    advanced
1    143.142267  9.397000    advanced
2    9.641728    9.397995    advanced
3    0.100000    9.397996    newbie
4    19.654137   9.399900    expert

在上面的示例中,前两行具有相同的 scoretype ,因此应将这些行合并在一起并将它们的分数相加。

期望的输出

     count       score       type
0    537.331573  9.397000    advanced
1    9.641728    9.397995    advanced
2    0.100000    9.397996    newbie
3    19.654137   9.399900    expert

最佳答案

这是groupby的工作:

>>> df.groupby(["score", "type"]).sum()
                        count
score    type                
9.397000 advanced  537.331573
9.397995 advanced    9.641728
9.397996 newbie      0.100000
9.399900 expert     19.6541374
>>> df.groupby(["score", "type"], as_index=False).sum()
      score      type       count
0  9.397000  advanced  537.331573
1  9.397995  advanced    9.641728
2  9.397996    newbie    0.100000
3  9.399900    expert   19.654137

关于python - 总结 Pandas DataFrame 中的列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20181456/

相关文章:

python - pandas 如何在具有不均匀列的数据框中复制行,仅当数据存在于特定列中时,并形成具有偶数列的新数据框?

python - 在 tkinter 中单击时突出显示文本

python - pandas DataFrame.drop 函数中的整数参数

Python 注释 : difference between Tuple and ()

python - 连续绘图(opencv)

python - 如何在 python 中使用 pylint 获取简短摘要或错误和警告的确切数量

python - Groupby 总和、索引与列结果

python - 如何仅允许某些文件语法使用 Sublime Text 3 包?

python-2.7 - pandas:按二级索引范围对 MultiIndex DataFrame 进行切片

python - 将多个字典附加到 Pandas 数据帧 : Error DataFrame constructor not properly called?