如何获取计数值较少的行的索引。例如:
test = pd.DataFrame({'price' : [1, 2, 1, 3, 5, 2, 2]})
out[1]:
price
0 1
1 2
2 1
3 3
4 5
5 2
6 2
如何获得价格 = 1, 3 ,5 的索引,该索引在列中仅出现少于 3 次?
最佳答案
使用重复
test[~test.price.duplicated(keep=False)]
price
3 3
4 5
test.index[~test.price.duplicated(keep=False)]
Int64Index([3, 4], dtype='int64')
更新然后你需要转换
test[test.groupby('price').price.transform('count')<=1]
price
3 3
4 5
test[test.groupby('price').price.transform('count')<3].index
Int64Index([0, 2, 3, 4], dtype='int64')
关于python - 如何获取 pandas 中出现频率较低的行的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54084867/