Skip to content

Instantly share code, notes, and snippets.

@thekvs
Created November 15, 2015 12:00
Show Gist options
  • Save thekvs/793d04bdb1f3d0a0ab54 to your computer and use it in GitHub Desktop.
Save thekvs/793d04bdb1f3d0a0ab54 to your computer and use it in GitHub Desktop.
select and change values in the data frame using frequency criteria
import pandas as pd
df = pd.DataFrame({'a': [1, 2, 3, 3, 4, 1, 7, 8, 9], 'b': [1, 1, 1, 1, 4, 4, 2, 3, 1]})
print(df)
print("========")
cidx = df.b.value_counts() <= 1
df.loc[df.b.isin(cidx.index[cidx]), 'b'] = 9999
print(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment