After a : df['A'].value_counts()
How can I replace all the values < 2 by for example "Others"
To have after it for example a line named "Others = 8"
CodePudding user response:
Use a boolean mask and append
:
out = df['A'].value_counts()
m = out < 2
out[~m].append(pd.Series(out[m].sum(), index=['Others']))
CodePudding user response:
Try this -
- Use groupby to group over column
A
and transform the column by itscount
, which is same as value counts but has same number of rows as original data - Use
lt(2)
which is less that 2 to get a boolean series - Update the column
A
for those indexes with the valueothers
idx = df.groupby(['A'])['A'].transform('count').lt(2)
df.loc[idx, 'A'] = 'others'
Edited with the loc
as suggested by @Henry. Thanks!