I am trying to sort a DataFrame (axis = 0) by another Series that is sorted in a specific order.
Example: DataFrame contains an index of CountryCodes: 'AUS', 'BWA' .... (Sorted Alphabetically) Series contains a list of CountryCodes and it's associated GDP (Sorted by GDP)
I can use DataFrame.join(Series) no problem and then sort the column 'GDP' and then del DF['GDP'] but is there a way of doing this directly without joining the structures?
You can reindex
by the index of the (sorted) Series:
In [1]: df = pd.DataFrame([[1, 2], [3, 4]], index=list('ab'))
In [2]: s = pd.Series([2,1], index=list('ab'))
In [3]: s
Out[3]:
a 2
b 1
In [4]: s.sort()
In [5]: df.reindex(s.index)
Out[5]:
0 1
b 3 4
a 1 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With