Last active
February 13, 2021 15:56
-
-
Save cjauvin/d82dc3c2a16e951707979ae3b234c51c to your computer and use it in GitHub Desktop.
HRU Deduping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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