Issue
I have a list of lists
tours = [[0, 4], [0, 5], [0, 6], [1, 13], [2, 0], [3, 8], [4, 9], [5, 10],
[6, 7], [7, 1], [8, 2], [9, 3], [10, 11], [11, 14], [12, 0], [13, 12], [14, 0]]
I also have a dataframe df
:
Node X Y Demand Profit
1 2 5.7735 0.00 40.0 16.0
2 3 2.8867 5.00 40.0 16.0
3 4 -2.8868 5.00 40.0 16.0
4 5 -5.7735 0.00 40.0 16.0
5 6 -2.8867 -5.00 40.0 16.0
6 7 2.8868 -5.00 40.0 16.0
7 8 8.6603 5.00 40.0 24.0
8 9 0.0000 10.00 40.0 24.0
9 10 -8.6603 5.00 40.0 24.0
10 11 -8.6603 -5.00 40.0 24.0
11 12 0.0000 -10.00 40.0 24.0
12 13 8.6603 -5.00 40.0 24.0
13 14 5.3405 0.75 10.0 10.0
14 15 3.3198 4.25 10.0 10.0
15 16 6.4952 -1.25 10.0 11.0
In the list of lists "tour" the first element of a list member defines the element in column X
and the second element of the list member defines the element in column Y
.
For example [0,4]
points to X=5.7735
, Y= -5.00
.
I need to create a new dataframe coord
with two columns X
and Y
where each row represents an X
,Y
pair defined by an element in tours
.
My plan is to create a dictionary and then create a new dataframe but I am struggling with getting column names to a new dataframe or having a duplicated index column and splitting the elements of the list.
Here is my code
d = {}
for t,tour in enumerate(tours):
xi = tour[0]
yi = tour[1]
key = t
d[key] = df["X"].iloc[xi], df["Y"].iloc[yi]
print(d)
coord = pd.DataFrame(d.items(),columns=['X', 'Y'])
print(coord)
Output:
{0: (5.7735, -5.0), 1: (5.7735, -5.0), 2: (5.7735, 5.0), 3: (2.8867, 4.25), 4: (-2.8868, 0.0), 5: (-5.7735, 5.0), 6: (-2.8867, -5.0), 7: (2.8868, -10.0), 8: (8.6603, 10.0), 9: (0.0, 5.0), 10: (-8.6603, 5.0), 11: (-8.6603, 0.0), 12: (0.0, -5.0), 13: (8.6603, -1.25), 14: (5.3405, 0.0), 15: (3.3198, 0.75), 16: (6.4952, 0.0)}
X Y
0 0 (5.7735, -5.0)
1 1 (5.7735, -5.0)
2 2 (5.7735, 5.0)
3 3 (2.8867, 4.25)
4 4 (-2.8868, 0.0)
5 5 (-5.7735, 5.0)
6 6 (-2.8867, -5.0)
7 7 (2.8868, -10.0)
8 8 (8.6603, 10.0)
9 9 (0.0, 5.0)
10 10 (-8.6603, 5.0)
11 11 (-8.6603, 0.0)
12 12 (0.0, -5.0)
13 13 (8.6603, -1.25)
14 14 (5.3405, 0.0)
15 15 (3.3198, 0.75)
16 16 (6.4952, 0.0)
I greatly appreciate your help!
My ultimate goal is to plot a full rout using X
and Y
as coordinates. Any advise will be greatly appreciated.
PS. Here is what I came up with. It works but I will appreciate any improvements:
d = {}
for t,tour in enumerate(tours):
xi = tour[0]
yi = tour[1]
key = t
d[key] = df["X"].iloc[xi], df["Y"].iloc[yi]
coord = pd.DataFrame.from_dict(d, orient='index', columns= ['X','Y'])
OUTPUT:
X Y
0 5.7735 -5.00
1 5.7735 -5.00
2 5.7735 5.00
3 2.8867 4.25
4 -2.8868 0.00
5 -5.7735 5.00
6 -2.8867 -5.00
7 2.8868 -10.00
8 8.6603 10.00
9 0.0000 5.00
10 -8.6603 5.00
11 -8.6603 0.00
12 0.0000 -5.00
13 8.6603 -1.25
14 5.3405 0.00
15 3.3198 0.75
16 6.4952 0.00
Is there an easier way that LoL to dict to dataframe ?
Solution
Here is another way to write the for loop so you do not get the tuple values as the Y column in the coord Dataframe:
coord_data = []
for pair in tours:
xidx, yidx = pair
x, y = df.loc[xidx, 'X'], df.loc[yidx, 'Y']
coord_data.append([x, y])
# Convert the list to a DataFrame
coord = pd.DataFrame(coord_data, columns=['X', 'Y'])
Answered By - Håkon Hægland
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.