I have a data frame that contains five columns for ID values, and some arbitrary metric. The ID values relate to 5 employees for a specific project, but there is no standard for the order that each ID is entered into the dataset. I want to perform a groupby on the set of 5 ID's to evaluate at a group level.
In[1]: df1 = pd.DataFrame({'ID_1' : [1, 1, 1, 1],
'ID_2' : [2, 2, 4, 4],
'ID_3' : [3, 3, 2, 2],
'ID_4' : [4, 5, 8, 8],
'ID_5' : [5, 4, 7, 7],
'some_metric' : [.7, .8, .2, .9]})
In[2]: print df1
Out[2]:
ID_1 ID_2 ID_3 ID_4 ID_5 some_metric
0 1 2 3 4 5 0.7
1 1 2 3 5 4 0.8
2 1 4 2 8 7 0.2
3 1 4 2 8 7 0.9
However due to the non-unique ordering of the ID's in the original dataset, if I perform a group by on the 5 ID's I will get three groups when there are only two unique groups.
In[3]: df1.groupby(['ID_1', 'ID_2', 'ID_3', 'ID_4', 'ID_5']).mean()
Out[3]:
ID_1 ID_2 ID_3 ID_4 ID_5 some_metric
1 2 3 4 5 0.70
5 4 0.80
4 2 8 7 0.55
Is there an easy way to group by unique values from the column? Or is there a way to pull out the data into a dictionary, and then back into a data frame?