利用pandas提取某个列中不重复项目

假设存在以下数据:
user_id age gender occupation zip_code
1 24 M technician 85711
2 53 F other 94043
3 23 M writer 32067
4 24 M technician 43537
5 33 F other 15213
6 42 M executive 98101
7 57 M administrator 91344
8 36 M administrator 05201
9 29 M student 01002
10 53 M lawyer 90703
需要查找occupation列中不重复项
users是已知数据框


 users['occupation'].value_counts()

输出每个不重复值的数量:
student 196
other 105
educator 95
administrator 79
engineer 67
programmer 66
librarian 51
writer 45
executive 32
scientist 31
artist 28
technician 27
marketing 26
entertainment 18
healthcare 16
retired 14
lawyer 12
salesman 12
none 9
homemaker 7
doctor 7
Name: occupation, dtype: int64

users['occupation'].value_counts().count()

输出一共含有多少个不重复值

另一种方式:

users.occupation.nunique()
    原文作者:hooyying
    原文地址: https://blog.csdn.net/hooyying/article/details/122974078
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞