I have a very large dataset containing the members in each team in each month. I want to find additions and deletions to each team. Because my dataset is very big, I'm trying to use in-built functions as much as possible.
My dataset looks like this:
month team members
0 0 A X, Y, Z
1 1 A X, Y
2 2 A W, X, Y
3 0 B D, E
4 1 B D, E, F
5 2 B F
It's generated by the following code:
num_months = 3
num_teams = 2
obs = num_months*num_teams
df = pd.DataFrame({"month": [i % num_months for i in range(obs)],
"team": ['AB'[i // num_months] for i in range(obs)],
"members": ["X, Y, Z", "X, Y", "W, X, Y", "D, E", "D, E, F", "F"]})
df
The result should be like this:
month team members additions deletions
0 0 A X, Y, Z None None
1 1 A X, Y None Z
2 2 A W, X, Y W None
3 0 B D, E None None
4 1 B D, E, F F None
5 2 B F None D, E
or in Python code
df = pd.DataFrame({"month": [i % num_months for i in range(obs)],
"team": ['AB'[i // num_months] for i in range(obs)],
"members": ["X, Y, Z", "X, Y", "W, X, Y", "D, E", "D, E, F", "F"],
"additions": [None, None, "W", None, "F", None],
"deletions": [None, "Z", None, None, None, "D, E"]
})
A technique that immediately comes to mind is to create a new column which shows the lagged value of members in each group, followed by taking the set difference (both ways) between both columns.
Is there a way to take set differences between columns using pandas inbuilt functions?
Are there other techniques I should try?