pandas的值计数,唯一值,成员资格

转自:https://blog.csdn.net/sinat_29957455/article/details/79007668
1、Series唯一值判断

[python]  view plain  copy
  1. s = Series([3,3,1,2,4,3,4,6,5,6])  
  2. #判断Series中的值是否重复,False表示重复  
  3. print(s.is_unique)  
  4. #False  
  5. #输出Series中不重复的值,返回值没有排序,返回值的类型为数组  
  6. print(s.unique())  
  7. #[3 1 2 4 6 5]  
  8. print(type(s.unique()))  
  9. #<class 'numpy.ndarray'>  
  10. #统计Series中重复值出现的次数,默认是按出现次数降序排序  
  11. print(s.value_counts())  
  12. ''''' 
  13. 3    3 
  14. 6    2 
  15. 4    2 
  16. 5    1 
  17. 2    1 
  18. 1    1 
  19. '''  
  20. #按照重复值的大小排序输出频率  
  21. print(s.value_counts(sort=False))  
  22. ''''' 
  23. 1    1 
  24. 2    1 
  25. 3    3 
  26. 4    2 
  27. 5    1 
  28. 6    2 
  29. '''  
2、成员资格判断

a、Series的成员资格

[python]  view plain  copy
  1. s = Series([5,5,6,1,1])  
  2. print(s)  
  3. ''''' 
  4. 0    5 
  5. 1    5 
  6. 2    6 
  7. 3    1 
  8. 4    1 
  9. '''  
  10. #判断矢量化集合的成员资格,返回一个bool类型的Series  
  11. print(s.isin([5]))  
  12. ''''' 
  13. 0     True 
  14. 1     True 
  15. 2    False 
  16. 3    False 
  17. 4    False 
  18. '''  
  19. print(type(s.isin([5])))  
  20. #<class 'pandas.core.series.Series'>  
  21. #通过成员资格方法选取Series中的数据子集  
  22. print(s[s.isin([5])])  
  23. ''''' 
  24. 0    5 
  25. 1    5 
  26. '''  
b、DataFrame的成员资格

[python]  view plain  copy
  1. a = [[3,2,6],[2,1,4],[6,2,5]]  
  2. data = DataFrame(a,index=["a","b","c"],columns=["one","two","three"])  
  3. print(data)  
  4. ''''' 
  5.    one  two  three 
  6. a    3    2      6 
  7. b    2    1      4 
  8. c    6    2      5 
  9. '''  
  10. #返回一个bool的DataFrame  
  11. print(data.isin([1]))  
  12. ''''' 
  13.      one    two  three 
  14. a  False  False  False 
  15. b  False   True  False 
  16. c  False  False  False 
  17. '''  
  18. #选取DataFrame中值为1的数,其他的为NaN  
  19. print(data[data.isin([1])])  
  20. ''''' 
  21.    one  two  three 
  22. a  NaN  NaN    NaN 
  23. b  NaN  1.0    NaN 
  24. c  NaN  NaN    NaN 
  25. '''  
  26. #将NaN用0进行填充  
  27. print(data[data.isin([1])].fillna(0))  
  28. ''''' 
  29.    one  two  three 
  30. a  0.0  0.0    0.0 
  31. b  0.0  1.0    0.0 
  32. c  0.0  0.0    0.0 
  33. '''  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值