Home > Blockchain >  Replace values < 2 by something else
Replace values < 2 by something else

Time:10-14

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"

enter image description here

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 -

  1. Use groupby to group over column A and transform the column by its count, which is same as value counts but has same number of rows as original data
  2. Use lt(2) which is less that 2 to get a boolean series
  3. Update the column A for those indexes with the value others
idx = df.groupby(['A'])['A'].transform('count').lt(2)
df.loc[idx, 'A'] = 'others'

Edited with the loc as suggested by @Henry. Thanks!

  • Related