Zip, sorted and pandas

I have a pandas data frame with column values ​​as follows:

names = wine_df.columns
names
Index([u'fixed acidity', u'volatile acidity', u'citric acid', u'residual sugar', u'chlorides', u'free sulfur dioxide', u'total sulfur dioxide', u'density', u'pH', u'sulphates', u'alcohol'], dtype='object')

I have a numpy array named imp with the following values:

array([ 0.07640909,  0.11346059,  0.09160943,  0.06674312,  0.07203855,
        0.06306923,  0.08272078,  0.0839144 ,  0.05996705,  0.11833288,
        0.17173489])

I was working on a project, and I came across this piece of code shown below:

zip(*sorted(zip(imp, names)))

I could not understand why they use * sorted inside a zip function? Also why do they use the zip function twice?

+4
source share
1 answer

The best way to see what he is doing is a simple example:

In [11]: a = np.array([2, 1, 3])

In [12]: a = np.array([2, 1, 2, 3])

In [13]: b = np.array(['b', 'b', 'a', 'c'])

In [14]: sorted(zip(a, b))
Out[14]: [(1, 'b'), (2, 'a'), (2, 'b'), (3, 'c')]

In [15]: zip(*sorted(zip(a, b)))
Out[15]: [(1, 2, 2, 3), ('b', 'a', 'b', 'c')]

It sorts both lists / arrays with respect to the values ​​in the first (they are followed by the values ​​in the second).

"" argsort ( ):

In [21]: s = np.argsort(a)

In [22]: a[s], b[s]
Out[22]:
(array([1, 2, 2, 3]), array(['b', 'b', 'a', 'c'],
       dtype='|S1'))

: , .

+2