python - 在重复行中对 DataFrame 中特定列的值求和
问题描述
我有一个数据框架,其中包含我删除并修改了一些信息的书籍。但是,“bookISBN”列中有一些行具有重复值,我想将所有这些行合并为一个。
我计划创建一个新的 DataFrame,在其中保留 url、ISBN、标题和流派的第一个值,但我想对“genreVotes”列的值求和以创建合并。我怎样才能做到这一点?
原始数据框:
In [23]: network = data[["bookTitle", "bookISBN", "highestVotedGenre", "genreVotes"]]
network.head().to_dict("list")
Out [23]:
{'bookTitle': ['The Hunger Games',
'Twilight',
'The Book Thief',
'Animal Farm',
'The Chronicles of Narnia'],
'bookISBN': ['9780439023481',
'9780316015844',
'9780375831003',
'9780452284241',
'9780066238500'],
'highestVotedGenre': ['Young Adult',
'Young Adult',
'Historical-Historical Fiction',
'Classics',
'Fantasy'],
'genreVotes': [103407, 80856, 59070, 73590, 26376]}
重复:
In [24]: duplicates = network[network.duplicated(subset=["bookISBN"], keep=False)]
duplicates.loc[(duplicates["bookISBN"] == "9780439023481") | (duplicates["bookISBN"] == "9780375831003")]
Out [24]:
{'bookTitle': ['The Hunger Games',
'The Book Thief',
'The Hunger Games',
'The Book Thief',
'The Book Thief'],
'bookISBN': ['9780439023481',
'9780375831003',
'9780439023481',
'9780375831003',
'9780375831003'],
'highestVotedGenre': ['Young Adult',
'Historical-Historical Fiction',
'Young Adult',
'Historical-Historical Fiction',
'Historical-Historical Fiction'],
'genreVotes': [103407, 59070, 103407, 59070, 59070]}
(在这个例子中,投票都是一样的,但在某些情况下,值是不同的)。
预期输出:
{'bookTitle': ['The Hunger Games',
'Twilight',
'The Book Thief',
'Animal Farm',
'The Chronicles of Narnia'],
'bookISBN': ['9780439023481',
'9780316015844',
'9780375831003',
'9780452284241',
'9780066238500'],
'highestVotedGenre': ['Young Adult',
'Young Adult',
'Historical-Historical Fiction',
'Classics',
'Fantasy'],
'genreVotes': [260814, 80856, 177210, 73590, 26376]}
解决方案
推荐阅读
- python - python尝试除了不工作
- android - 在 androidx.recyclerview.widget.RecyclerView 上找不到参数类型为布尔型的属性“app:fastScrollEnabled”的设置器
- android - 如何使用 Appium 单击/点击嵌入在文本消息中的链接?
- java - 如何避免Java中的方法重载以避免重复?
- python - 将组否定为 [^(?:\n\n\n\n)]+
- regex - 使用 powershell 和正则表达式查找包含特定字符串的所有文件
- selenium - org.openqa.selenium.NoAlertPresentException:通过 Selenium 调用 Actions 类方法 sendKeys 时没有打开警报
- psql - 关于postgreSQL命令的一个问题
- python-3.x - 在python中将struct_time转换为String有错误
- html - Put elements of a div in a css box