小编Lux*_*_Jr的帖子

一列列表上的 Pandas groupby

我有一个pandas包含列的数据框lists

df = pd.DataFrame({'List': [['once', 'upon'], ['once', 'upon'], ['a', 'time'], ['there', 'was'], ['a', 'time']], 'Count': [2, 3, 4, 1, 2]})

Count   List
2    [once, upon]
3    [once, upon]
4    [a, time]
1    [there, was]
2    [a, time]
Run Code Online (Sandbox Code Playgroud)

如何组合List列并对列求和Count?预期的结果是:

Count   List
5     [once, upon]
6     [a, time]
1     [there, was]
Run Code Online (Sandbox Code Playgroud)

我试过了:

df.groupby('List')['Count'].sum()
Run Code Online (Sandbox Code Playgroud)

这导致:

TypeError: unhashable type: 'list'
Run Code Online (Sandbox Code Playgroud)

python python-3.x pandas pandas-groupby

4
推荐指数
1
解决办法
4353
查看次数

基于条件累积和的多个 Pandas 列

我有一个dataframe包含多个“堆栈”及其相应的“长度”。

df = pd.DataFrame({'stack-1-material': ['rock', 'paper', 'paper', 'scissors', 'rock'], 'stack-2-material': ['rock', 'paper', 'rock', 'paper', 'scissors'], 'stack-1-length': [3, 1, 1, 2, 3], 'stack-2-length': [3, 1, 3, 1, 2]})

  stack-1-material stack-2-material  stack-1-length  stack-2-length
0             rock             rock               3               3
1            paper            paper               1               1
2            paper             rock               1               3
3         scissors            paper               2               1
4             rock         scissors               3               2
Run Code Online (Sandbox Code Playgroud)

我试图为每种材料创建一个单独的列,跟踪长度的累积总和,而不管它们是哪个“堆栈”。我试过使用,groupby但只能将累积总和放入一列。这是我要找的:

  stack-1-material stack-2-material  stack-1-length  stack-2-length  rock_cumsum  paper_cumsum  scissors_cumsum
0             rock             rock               3               3            6             0                0
1            paper            paper               1 …
Run Code Online (Sandbox Code Playgroud)

cumulative-sum python-3.x pandas

2
推荐指数
1
解决办法
41
查看次数