Skip to content Skip to sidebar Skip to footer

Python Dataframe Interaction

I have the following dataframe: topic student level week 1 a 1 1 1 b 2 1 1 a 3 1 2 a 1 2 2 b 2 2 2

Solution 1:

result = (df.groupby('week').apply(
        lambda g: g.groupby([g.student.shift(), g.student])
        .week.agg({'reply_count': 'count'})
        .rename_axis(("st_source", "st_dest"))
    ).reset_index())
​
result[['st_source', 'st_dest', 'week', 'reply_count']].sort_values(['st_source', 'st_dest'])

# st_source     st_dest   week  reply_count
#0        a         b        1          1
#2        a         b        2          2
#1        b         a        1          1
#3        b         a        2          1

Post a Comment for "Python Dataframe Interaction"