I have a question about the Python procession.

Asked 2 years ago, Updated 2 years ago, 43 views

Hello, I'd like to ask you a question about the procession. For example, if the order of 'a', 'b', 'c', 'd', and 'e' is changed in the code below, I want the order of elements in the list to be connected and rearranged together, what should I do?

index=['a','b','c','d','e']
lst = [[0,0,1,0,1,0],
       [0,1,1,0,0,0],
       [1,0,0,1,0,0],
       [0,1,1,0,1,0],
       [1,0,0,1,0,1]]
#print output
index=['b','a','c','d','e'] #After changing,
lst = [[0,0,1,0,1,0], I want to make the order of lst change when the order of #index changes.
       [1,0,1,0,0,0],
       [0,1,0,1,0,0],
       [1,0,1,0,1,0],
       [0,1,0,1,0,1]]

python python-2.x

2022-09-22 17:56

1 Answers

Maybe six...

Matrix operations are easy to work with using pandas and numpy.

You can work as follows:

index=['a','b','c','d','e','f']
lst = [[0,0,1,0,1,0],
       [0,1,1,0,0,0],
       [1,0,0,1,0,0],
       [0,1,1,0,1,0],
       [1,0,0,1,0,1]]

import pandas

pd = pandas.DataFrame(lst, columns=index)
print(pd)
print(pd.reindex(columns=['b','a','c','d','e','f']))

   a  b  c  d  e  f
0  0  0  1  0  1  0
1  0  1  1  0  0  0
2  1  0  0  1  0  0
3  0  1  1  0  1  0
4  1  0  0  1  0  1

   b  a  c  d  e  f
0  0  0  1  0  1  0
1  1  0  1  0  0  0
2  0  1  0  1  0  0
3  1  0  1  0  1  0
4  0  1  0  1  0  1


2022-09-22 17:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.