I have a dataframe that has two columns: label and value. I would like to identify the number of unique values in the dataframe that occurs in each label group.
For example, given the following dataframe:
test_df = pd.DataFrame({
'label': [1, 1, 1, 1, 2, 2, 3, 3, 3],
'value': [0, 0, 1, 2, 1, 2, 2, 3, 4]})
test_df
label value
0 1 0
1 1 0
2 1 1
3 1 2
4 2 1
5 2 2
6 3 2
7 3 3
8 3 4
The expected output is:
label uni_val
0 1 1 -> {0} is unique value for this label compared to other labels
1 2 0 -> no unique values for this label compared to other labels
2 3 2 -> {3, 4} are unique values for this label compared to other labels
One way of doing this is to get the unique values for each label and then count the non-duplicates of them across all elements.
test_df.groupby('label')['value'].unique()
label
1 [0, 1, 2]
2 [1, 2]
3 [2, 3, 4]
Name: value, dtype: object
Is there a more efficient and simpler way?
You could drop duplicates on ['label', 'value']
, then drop duplicates on value
:
(test_df.drop_duplicates(['label','value']) # remove duplicates on pair (label, value)
.drop_duplicates('value', keep=False) # only keep unique `value`
.groupby('label')['value'].count() # count as usual
.reindex(test_df.label.unique(), fill_value=0) # fill missing labels with 0
)
Output:
label
1 1
2 0
3 2
Name: value, dtype: int64
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.