I have a dataframe:
topic student level
1 a 1
1 b 2
1 a 3
2 a 1
2 b 2
2 a 3
2 b 4
3 c 1
3 b 2
3 c 3
3 a 4
3 b 5
It contains a column level that specifies who started the topic and who replied to it. If a level is 1, it means that a student started the topic. If a level is 2, it means that a student replied to student who started the topic. If a level is 3, it means that a student replied to student at level 2 and on and on.
I would like to extract a new dataframe that should present a communication between students through the topic. It should contain three columns: "student source", "student destination" and "reply count". Reply count is a number of times in which Student Destination "directly" replied to Student Source.
I should get something like:
st_source st_dest reply_count
a b 4
a c 0
b a 2
b c 1
c a 1
c b 1
I tried to find first two columns using this code..
idx_cols = ['topic']
std_cols = ['student_x', 'student_y']
df1 = df.merge(df, on=idx_cols)
df2 = df1.loc[f1.student_x != f1.student_y, idx_cols + std_cols]
df2.loc[:, std_cols] = np.sort(df2.loc[:, std_cols])
Does anyone have some suggestions for the third column?
Thank you in advance!