Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reordering nodes in increasing 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 the node1 and node 2 in the data1 according in increasing order. Nodes are 2 3 6 7 28 so they become 1 2 3 4 5 respectively.

So the dataframe 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 before

enter image description here

but now looks like this

enter image description here

like image 716
ubuntu_noob Avatar asked Dec 04 '17 14:12

ubuntu_noob


Video Answer


1 Answers

Factorizing by sorting and assigning by reshaping i.e

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 factorize 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
like image 178
Bharath Avatar answered Sep 30 '22 21:09

Bharath