我有一个包含两列的数据框。一列包含名称,另一列包含不同的类别。
import pandas as pd
data={"name":["Jack", "Mary", "John", "Derek", "Jack", "Jack"], "category":["c1","c2","c3","c1","c1","c2"]}
df=pd.DataFrame(data=data)
我想对名称和类别列进行分组,以便为每个人的每个类别的计数创建新列。
所需的输出:
name count_of_c1 count_of_c2 count_of_c3
Jack 2 1 0
Mary 0 1 0
John 0 0 1
Derek 1 0 0
熊猫中有解决此问题的功能吗? 谢谢!