如何将行添加为 DataFrame 中其他行的总和?
How to add rows as sums of other rows in DataFrame?
我不确定我给这个 post 的标题是否正确,但我有一个独特的情况,我想将一组新的行作为现有集合中的行的总和附加到现有的 DataFrame 并且我'我不知道从哪里开始。
例如,我有以下 DataFrame:
import pandas as pd
data = {'Team': ['Atlanta', 'Atlanta', 'Cleveland', 'Cleveland'],
'Position': ['Defense', 'Kicker', 'Defense', 'Kicker'],
'Points': [5, 10, 15, 20]}
df = pd.DataFrame(data)
print(df)
Team Position Points
0 Atlanta Defense 5
1 Atlanta Kicker 10
2 Cleveland Defense 15
3 Cleveland Kicker 20
如何 create/append 新行为每个团队创建一个新位置并为每个团队计算两个现有位置的分数?此外,完整的数据集由多个团队组成,因此我正在寻找适用于任意数量团队的解决方案。
edit: 我忘了包括完整的DataFrame中还有其他位置;但是,我只想将此解决方案应用于“防御”和“踢球”位置。
我想要的输出如下。
Team Position Points
Team Position Points
0 Atlanta Defense 5
1 Atlanta Kicker 10
2 Cleveland Defense 15
3 Cleveland Kicker 20
4 Atlanta Defense + Kicker 15
5 Cleveland Defense + Kicker 35
提前致谢!
我们可以对DataFrame使用groupby agg
to create the summary rows then append
:
df = df.append(df.groupby('Team', as_index=False).agg({
'Position': ' + '.join, # Concat Strings together
'Points': 'sum' # Total Points
}), ignore_index=True)
df
:
Team Position Points
0 Atlanta Defense 5
1 Atlanta Kicker 10
2 Cleveland Defense 15
3 Cleveland Kicker 20
4 Atlanta Defense + Kicker 15
5 Cleveland Defense + Kicker 35
我们还可以通过在 groupby
之前过滤 df
来将某些职位列入白名单,以仅聚合所需的职位:
whitelisted_positions = ['Kicker', 'Defense']
df = df.append(
df[df['Position'].isin(whitelisted_positions)]
.groupby('Team', as_index=False).agg({
'Position': ' + '.join, # Concat Strings together
'Points': 'sum' # Total Points
}), ignore_index=True
)
我不确定我给这个 post 的标题是否正确,但我有一个独特的情况,我想将一组新的行作为现有集合中的行的总和附加到现有的 DataFrame 并且我'我不知道从哪里开始。
例如,我有以下 DataFrame:
import pandas as pd
data = {'Team': ['Atlanta', 'Atlanta', 'Cleveland', 'Cleveland'],
'Position': ['Defense', 'Kicker', 'Defense', 'Kicker'],
'Points': [5, 10, 15, 20]}
df = pd.DataFrame(data)
print(df)
Team Position Points
0 Atlanta Defense 5
1 Atlanta Kicker 10
2 Cleveland Defense 15
3 Cleveland Kicker 20
如何 create/append 新行为每个团队创建一个新位置并为每个团队计算两个现有位置的分数?此外,完整的数据集由多个团队组成,因此我正在寻找适用于任意数量团队的解决方案。
edit: 我忘了包括完整的DataFrame中还有其他位置;但是,我只想将此解决方案应用于“防御”和“踢球”位置。
我想要的输出如下。
Team Position Points
Team Position Points
0 Atlanta Defense 5
1 Atlanta Kicker 10
2 Cleveland Defense 15
3 Cleveland Kicker 20
4 Atlanta Defense + Kicker 15
5 Cleveland Defense + Kicker 35
提前致谢!
我们可以对DataFrame使用groupby agg
to create the summary rows then append
:
df = df.append(df.groupby('Team', as_index=False).agg({
'Position': ' + '.join, # Concat Strings together
'Points': 'sum' # Total Points
}), ignore_index=True)
df
:
Team Position Points
0 Atlanta Defense 5
1 Atlanta Kicker 10
2 Cleveland Defense 15
3 Cleveland Kicker 20
4 Atlanta Defense + Kicker 15
5 Cleveland Defense + Kicker 35
我们还可以通过在 groupby
之前过滤 df
来将某些职位列入白名单,以仅聚合所需的职位:
whitelisted_positions = ['Kicker', 'Defense']
df = df.append(
df[df['Position'].isin(whitelisted_positions)]
.groupby('Team', as_index=False).agg({
'Position': ' + '.join, # Concat Strings together
'Points': 'sum' # Total Points
}), ignore_index=True
)