如何获取熊猫中具有较低频繁值的行的索引

时间:2019-01-08 03:32:23

标签: python pandas dataframe

如何获取计数值较少的行的索引。例如:

std::string p1_move = ask_user("Player 1 move (R/P/S)?\n", RPS);

如何获取价格指数= 1、3、5,而该价格指数在该列中的出现次数少于3次?

2 个答案:

答案 0 :(得分:2)

使用duplicated

test[~test.price.duplicated(keep=False)]
   price
3      3
4      5

test.index[~test.price.duplicated(keep=False)]
Int64Index([3, 4], dtype='int64')

更新,则您需要transform

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')

答案 1 :(得分:2)

您可以用value_counts计算项目,然后选择“稀有”的项目:

THRESHOLD = 3
is_rare = test['price'].value_counts() < THRESHOLD
rare = is_rare[is_rare].index
#Int64Index([1, 5, 3], dtype='int64')

接下来,找到包含稀有物品的行:

are_rare_rows = test["price"].isin(rare)
are_rare_rows[are_rare_rows].index
#Int64Index([0, 2, 3, 4], dtype='int64')