Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Using a pd.Series to sort a pd.DataFrame with index

Tags:

python

pandas

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?

like image 877
sanguineturtle Avatar asked Oct 04 '22 23:10

sanguineturtle


1 Answers

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
like image 139
Andy Hayden Avatar answered Oct 10 '22 01:10

Andy Hayden