Advertisement
hakonhagland

python-pandas-create-df

Dec 25th, 2023
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. 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]]
  4.  
  5. data = [
  6.     [2, 5.7735, 0.00, 40.0, 16.0],
  7.     [3, 2.8867, 5.00, 40.0, 16.0],
  8.     [4, -2.8868, 5.00, 40.0, 16.0],
  9.     [5, -5.7735, 0.00, 40.0, 16.0],
  10.     [6, -2.8867, -5.00, 40.0, 16.0],
  11.     [7, 2.8868, -5.00, 40.0, 16.0],
  12.     [8, 8.6603, 5.00, 40.0, 24.0],
  13.     [9, 0.0000, 10.00, 40.0, 24.0],
  14.     [10, -8.6603, 5.00, 40.0, 24.0],
  15.     [11, -8.6603, -5.00, 40.0, 24.0],
  16.     [12, 0.0000, -10.00, 40.0, 24.0],
  17.     [13, 8.6603, -5.00, 40.0, 24.0],
  18.     [14, 5.3405, 0.75, 10.0, 10.0],
  19.     [15, 3.3198, 4.25, 10.0, 10.0],
  20.     [16, 6.4952, -1.25, 10.0, 11.0]
  21. ]
  22.  
  23. df = pd.DataFrame(data, columns=['Node', 'X', 'Y', 'Demand', 'Profit'])
  24.  
  25. # Creating the new DataFrame
  26. coord_data = []
  27. for pair in tours:
  28.     xidx, yidx = pair
  29.     x, y = df.loc[xidx, 'X'], df.loc[yidx, 'Y']
  30.     coord_data.append([x, y])
  31.  
  32. # Convert the list to a DataFrame
  33. coord = pd.DataFrame(coord_data, columns=['X', 'Y'])
  34.  
  35. print(coord)
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement