Sorting with Pandas does not return the desired result

Assume that the Gfollowing identifier is specified for the network (Node ID, number of links) and indicate that you want to reset it to the Pandas DataFrame:

import pandas as pd
import networkx as nx

degree=pd.DataFrame({'Node ID':G.degree().keys(),'Degree':G.degree().values()})
degree=degree[['Node ID','Degree']] #re-order

You get this:

In[1]: degree.head(5)
Out[1]:
     Node ID  Degree
0    0        19
1    1        117
2    2        13
3    3        56
4    4        15

Now say that you want to sort this DataFrame relative to the Degree column in descending order. If i do it

sort_degree=degree.sort_values(['Node ID', 'Degree'], ascending=[False, False], inplace=False)

I do not get what I want:

    Node ID  Degree
4   4        15
3   3        56
2   2        13
1   1        117
0   0        19

What's wrong?

+4
source share
1 answer

It seems to me that you only need to sort the “Degree” column:

In [156]:
sort_df = df.sort_values('Degree', ascending=False)
sort_df

Out[156]:
   Node ID  Degree
1        1     117
3        3      56
0        0      19
4        4      15
2        2      13

, , , . : http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html#pandas.DataFrame.sort_values

+4

Source: https://habr.com/ru/post/1658343/


All Articles