Home Obtain a difference variable based on all posible combinations of another one in Python
 I have a data set which contains a date variable, two id variables, and a numeric variable. For example:  date id1 id2 var1 0 201012 1 A 13.7 1 201012 1 B 10.0 2 201012 1 C 0.0 3 201012 2 A 2.0  I wish to obtain all possible (not ordered) combinations for the relationship date-id2 per id1, and retrieve the difference in var1. What I would like to obtain is a new dataframe which looks like this:  relation newvar 0 201012-1-A-B 3.7 1 201012-1-A-C 13.7 2 201012-1-B-C 10.0  That is, if id1 has a relationship with say three different values of id2, i want to find such pair combinations and obtain the difference in the numeric value. In the example, id1=1 has a relation with id2=A,B,C, so i want to obtain the combinations A-B, A-C, B-C, which would lead to (13.7 minus 10.0), (13.7 minus 0.0) and (10.0 minus 0.0). Thanks!