I have a dataframe with 2 columns: date and reference. I want to sort the data such that it is sorted by date, but all rows with the same reference are next to each other. The data would be sorted according to the date of the earliest row in a set of identical references. So for example:
date | ref |
---|---|
1 | rabbit |
2 | frog |
3 | frog |
4 | rabbit |
4 | bear |
5 | rabbit |
5 | bear |
would become
date | ref |
---|---|
1 | rabbit |
4 | rabbit |
5 | rabbit |
2 | frog |
3 | frog |
4 | bear |
5 | bear |
How can I do that? I've tried 2 methods already:
- sorting by date and then going down the "ref" column and swapping rows that have a match, which didn't work, and
- sorting by date and then swapping "ref" names with each other based on date, and then sorting by ref alphabetically (I don't need to keep the ref names, they just need to match up) Nothing worked. What's the way to do this?