Reordering nodes in ascending order in pandas dataframe

data1 = { 'node1': [2,2,3,6], 'node2': [6,7,7,28], 'weight': [1,2,1,1], } df1 = pd.DataFrame(data1, columns = ['node1','node2','weight']) 

I want to rename node1 and node 2 to data1 in ascending order. The nodes are 2 3 6 7 28, so they become 1 2 3 4 5, respectively.

So the data frame becomes -

 data1 = { 'node1': [1,1,2,3], 'node2': [3,4,4,5], 'weight': [1,2,1,1], } df1 = pd.DataFrame(data1, columns = ['node1','node2','weight']) 

The data looked like this:

enter image description here

but now it looks like

enter image description here

+5
source share
3 answers

Factorization by sorting and assigning by changing the form ie

 df1[['node1','node2']] = (pd.factorize(np.sort(df1[['node1','node2']].values.reshape(-1)))[0]+1).reshape(-1,len(df1)).T node1 node2 weight 0 1 3 1 1 1 4 2 2 2 4 1 3 3 5 1 

Another approach with melt and factorization and renaming with dict

 vals = pd.factorize(df1[['node1','node2']].melt().sort_values('value')['value']) to_rename = dict(zip(vals[1],np.unique(vals[0]+1))) # {2: 1, 3: 2, 6: 3, 7: 4, 28: 5} df1[['node1','node2']] = df1[['node1','node2']].apply(lambda x : x.map(to_rename)) # Also df1[['node1','node2']] = df1[['node1','node2']].replace(to_rename) Thanks @jezrael node1 node2 weight 0 1 3 1 1 1 4 2 2 2 4 1 3 3 5 1 
+7
source

Use rank with changing the form of stack and then unstack :

 df2 = (df1.set_index('weight', append=True) .stack() .rank(method='dense') .astype(int) .unstack() .reset_index(level=1)) print (df2) weight node1 node2 0 1 1 3 1 2 1 4 2 1 2 4 3 1 3 5 
+5
source

Or we can use replace :-)

 ary=np.concatenate(df1.iloc[:,:2].values) mapdf=pd.DataFrame({'data':pd.Series(ary).astype('category').cat.codes.add(1),'maper':ary}).set_index('maper') df1[['node1','node2']]=df1[['node1','node2']].replace(mapdf.data.to_dict()) df1 Out[1631]: node1 node2 weight 0 1 3 1 1 1 4 2 2 2 4 1 3 3 5 1 
+1
source

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


All Articles