Replacing data with numpy

Asked 2 years ago, Updated 2 years ago, 16 views

X = {x1, x2, x3, x4, x5}
Y = {y1,y2,y3,y4,y5}
There is a series called
This data corresponds to x1 and y1, x2 and y2, respectively, and I would like to change the order in ascending order with the value of X without changing this combination. What should I do?

python

2022-09-30 10:38

1 Answers

From the expression series, assume that you are talking about Pandas.

>>import pandas as pd
>>>X = [1,5,2,3,4]
>>Y = [9,8,6,5,7]
>>>df=pd.DataFrame ({'X':X, 'Y':Y})
>>df
   XY
0  1  9
1  5  8
2  2  6
3  3  5
4  4  7
>> df.sort_values (by = 'X')
   XY
0  1  9
2  2  6
3  3  5
4  4  7
1  5  8

If you want to implement only python standard features without using Pandas, you can write as follows.

>>X2,Y2 = zip (*sorted(zip(X,Y)))
>>> X2
(1, 2, 3, 4, 5)
>>Y2
(9, 6, 5, 7, 8)


2022-09-30 10:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.