Skip to content

Instantly share code, notes, and snippets.

@cjauvin
Last active February 13, 2021 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjauvin/d82dc3c2a16e951707979ae3b234c51c to your computer and use it in GitHub Desktop.
Save cjauvin/d82dc3c2a16e951707979ae3b234c51c to your computer and use it in GitHub Desktop.
HRU Deduping
In [65]: import pandas as pd
In [66]: a = pd.DataFrame({'hru': [9, 10, 10], 'sub': [100, 100, 100], 'down': [-1, 200, -1]})
In [67]: a
Out[67]:
hru sub down
0 9 100 -1
1 10 100 200
2 10 100 -1
In [73]: a.sort_values(['hru', 'down'], ascending=True)
Out[73]:
hru sub down
0 9 100 -1
2 10 100 -1
1 10 100 200
In [74]: a.sort_values(['hru', 'down'], ascending=False)
Out[74]:
hru sub down
1 10 100 200
2 10 100 -1
0 9 100 -1
In [75]: a.sort_values(['hru', 'down'], ascending=True).drop_duplicates('hru', keep='first')
Out[75]:
hru sub down
0 9 100 -1
2 10 100 -1
In [76]: a.sort_values(['hru', 'down'], ascending=True).drop_duplicates('hru', keep='last')
Out[76]:
hru sub down
0 9 100 -1
1 10 100 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment