如何在 Pandas 数据透视表 Table 中聚合数据?
How to aggregate data in a Pandas Pivot Table?
我正在执行一项空间对齐任务,我正在探索不同 score/rescore 函数对对齐质量(通过 RMSD 测量)的影响。我有长格式数据,其中我有 运行 不同系统的所有评分/重新评分组合,并且重复了 3 次。
下面是一些示例测试数据:
identifier score rescore rmsd repeat
0 1abc plp asp 1.2 1
1 1abc plp asp 1.3 2
2 1abc plp asp 1.5 3
3 1abc plp plp 3.2 1
4 1abc plp plp 3.3 2
5 1abc plp plp 3.5 3
6 1abc asp asp 5.2 1
7 1abc asp asp 5.3 2
8 1abc asp asp 5.5 3
9 1abc asp plp 1.2 1
10 1abc asp plp 1.3 2
11 1abc asp plp 1.5 3
12 2def plp asp 1.0 1
13 2def plp asp 1.1 2
14 2def plp asp 1.2 3
15 2def plp plp 3.0 1
16 2def plp plp 3.1 2
17 2def plp plp 3.2 3
18 2def asp asp 5.0 1
19 2def asp asp 5.1 2
20 2def asp asp 5.2 3
21 2def asp plp 1.0 1
22 2def asp plp 1.3 2
23 2def asp plp 1.7 3
对于这个特定的任务,RMSD <= 1.5 被认为是成功的。我想计算我数据集中所有系统的成功率百分比,按分数和重新分数组合划分。我想将结果报告为 3 次重复的平均值和标准偏差(成功率百分比)。
期望的输出:
rescore asp plp
vals mean sd mean sd
score
asp 0.0 0.0 83.3 28.9
plp 100.0 0.0 0.0 0.0
我目前的尝试:
df = pd.read_csv("test.csv", index_col=0)
systems = len(set(df.identifier))
pd.pivot_table(df,
index='score',
columns= ['rescore', 'repeat'],
values='rmsd',
aggfunc=lambda x:((x <= 1.5).sum()/systems)*100)
输出:
rescore asp plp
repeat 1 2 3 1 2 3
score
asp 0.0 0.0 0.0 100.0 100.0 50.0
plp 100.0 100.0 100.0 0.0 0.0 0.0
所以我的问题是:
如何聚合 'repeat' 列以产生平均值和标准偏差?
您可以 .melt()
旋转 table 并再次旋转它。
systems = len(set(df.identifier))
pd.pivot_table(df,
index='score',
columns= ['rescore', 'repeat'],
values='rmsd',
aggfunc=lambda x:((x <= 1.5).sum()/systems)*100
).melt(ignore_index=False)\
.reset_index()\
.pivot_table(index='score',
columns='rescore',
values='value',
aggfunc=['mean', 'std'])
输出:
mean std
rescore asp plp asp plp
score
asp 0.000 83.333 0.000 28.868
plp 100.000 0.000 0.000 0.000
或者,您可以将 repeat
参数移动到 pd.pivot_table()
函数中的 index
参数并使用 .groupby()
方法。
systems = len(set(df.identifier))
pd.pivot_table(df,
index=['score', 'repeat'],
columns= 'rescore',
values='rmsd',
aggfunc=lambda x:((x <= 1.5).sum()/systems)*100
).reset_index()\
.groupby('score')[df['rescore'].unique()].agg(['mean', 'std'])\
.swaplevel(0,1,1)\
.sort_index(axis=1)
我正在执行一项空间对齐任务,我正在探索不同 score/rescore 函数对对齐质量(通过 RMSD 测量)的影响。我有长格式数据,其中我有 运行 不同系统的所有评分/重新评分组合,并且重复了 3 次。
下面是一些示例测试数据:
identifier score rescore rmsd repeat
0 1abc plp asp 1.2 1
1 1abc plp asp 1.3 2
2 1abc plp asp 1.5 3
3 1abc plp plp 3.2 1
4 1abc plp plp 3.3 2
5 1abc plp plp 3.5 3
6 1abc asp asp 5.2 1
7 1abc asp asp 5.3 2
8 1abc asp asp 5.5 3
9 1abc asp plp 1.2 1
10 1abc asp plp 1.3 2
11 1abc asp plp 1.5 3
12 2def plp asp 1.0 1
13 2def plp asp 1.1 2
14 2def plp asp 1.2 3
15 2def plp plp 3.0 1
16 2def plp plp 3.1 2
17 2def plp plp 3.2 3
18 2def asp asp 5.0 1
19 2def asp asp 5.1 2
20 2def asp asp 5.2 3
21 2def asp plp 1.0 1
22 2def asp plp 1.3 2
23 2def asp plp 1.7 3
对于这个特定的任务,RMSD <= 1.5 被认为是成功的。我想计算我数据集中所有系统的成功率百分比,按分数和重新分数组合划分。我想将结果报告为 3 次重复的平均值和标准偏差(成功率百分比)。
期望的输出:
rescore asp plp
vals mean sd mean sd
score
asp 0.0 0.0 83.3 28.9
plp 100.0 0.0 0.0 0.0
我目前的尝试:
df = pd.read_csv("test.csv", index_col=0)
systems = len(set(df.identifier))
pd.pivot_table(df,
index='score',
columns= ['rescore', 'repeat'],
values='rmsd',
aggfunc=lambda x:((x <= 1.5).sum()/systems)*100)
输出:
rescore asp plp
repeat 1 2 3 1 2 3
score
asp 0.0 0.0 0.0 100.0 100.0 50.0
plp 100.0 100.0 100.0 0.0 0.0 0.0
所以我的问题是:
如何聚合 'repeat' 列以产生平均值和标准偏差?
您可以 .melt()
旋转 table 并再次旋转它。
systems = len(set(df.identifier))
pd.pivot_table(df,
index='score',
columns= ['rescore', 'repeat'],
values='rmsd',
aggfunc=lambda x:((x <= 1.5).sum()/systems)*100
).melt(ignore_index=False)\
.reset_index()\
.pivot_table(index='score',
columns='rescore',
values='value',
aggfunc=['mean', 'std'])
输出:
mean std
rescore asp plp asp plp
score
asp 0.000 83.333 0.000 28.868
plp 100.000 0.000 0.000 0.000
或者,您可以将 repeat
参数移动到 pd.pivot_table()
函数中的 index
参数并使用 .groupby()
方法。
systems = len(set(df.identifier))
pd.pivot_table(df,
index=['score', 'repeat'],
columns= 'rescore',
values='rmsd',
aggfunc=lambda x:((x <= 1.5).sum()/systems)*100
).reset_index()\
.groupby('score')[df['rescore'].unique()].agg(['mean', 'std'])\
.swaplevel(0,1,1)\
.sort_index(axis=1)