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]]
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
© 2024 OneMinuteCode. All rights reserved.