我有这样的数据框:
df = pd.DataFrame({'id': [115,120,200], 'category': ['a','a', 'b'], 'clust': [1, 2, 3]})
我想汇总和计算每个类别的id数量,特别是clust。例如,结果也可以是数据框,其中索引行是clust而索引列是类别,值是id的数量
答案 0 :(得分:0)
IIUC,让我们使用<div id="map">
<div id="registermap">
<a href="register.html">
<img src="images/middle-earth_map1.png" alt="Registration Link" class="wobble" />
</a>
</div>
<div id="gollum">
<a href="cast.html">
<img src="images/gollum.png" alt="Cast and Crew Link" class="wobble" />
</a>
</div>
<div id="playtrailer">
<a href="trailer.html">
<img src="images/play_button.png" alt="Movie Trailer Link" class="wobble" />
</a>
</div>
</div>
和groupby
:
unstack
输入数据框:
import pandas as pd
df = pd.DataFrame({'id': [115,120,200], 'category': ['a', 'a', 'b'], 'clust': [1, 2, 3]})
df
分组,aggegrate和reshape:
category clust id
0 a 1 115
1 a 2 120
2 b 3 200
输出:
df_out = df.groupby(['clust','category'])['id'].count().unstack()