Код
import pandas as pd
import numpy as np
data=np.array([['','id','title','parent_id'],
[0,11,'p1',11],
[1,12,'p1',11],
[2,13,'p2',12],
[3,14,'p2',12],
[4,15,'p2',13],
[5,16,'p3',13],
[6,17,'p3',13]])
df=pd.DataFrame(data=data[1:,1:],
index=data[1:,0],
columns=data[0,1:])
df2=df.pivot(index='id',columns='title',values='parent_id')
df2=df.groupby(['title', 'parent_id']).count()
df2=df2.reset_index(drop=False)
df3=df2.pivot(index='parent_id',columns='title',values='id')
df3=df3.fillna(0)
print(df3)
Вход
id title parent_id
0 11 p1 11
1 12 p1 11
2 13 p2 12
3 14 p2 12
4 15 p2 13
5 16 p3 13
6 17 p3 13
Выход
title p1 p2 p3
parent_id
11 2.0 0.0 0.0
12 0.0 2.0 0.0
13 0.0 1.0 2.0