I have a dataset in wide format where the x and y ordinates for a line are in separate columns. The example below has only 4 ordinate pairs, but the actual dataset has a few dozen. There is a column with the number of ordinates (n in the example below).
How can I melt this dataframe with so many xn, yn columns? Can I do this without explicitly calling ('x1', 'y1', 'x2', 'y2', 'x3', 'y3', 'x4', 'y4' ... 'xn', 'yn')? I need to track the order, such that the (x1,y1) pair is counter 1; (x2,y2) is counter 2; etc.
idx = [1, 2, 3]
colA = [10, 5, 12]
n = [3, 2, 4]
x1 = [0, 1, 7]
y1 = [4, 0, 4]
x2 = [3, 2, 8]
y2 = [5, 1, 5]
x3 = [4, np.nan, 10]
y3 = [3,np.nan, 3]
x4 = [np.nan, np.nan, 11]
y4 = [np.nan, np.nan, 3]
df = pd.DataFrame(list(zip(idx, colA, n,
x1, y1, x2, y2, x3, y3, x4, y4
)),
columns =['idx', 'colA', 'n',
'x1', 'y1', 'x2', 'y2',
'x3', 'y3', 'x4', 'y4'
])
display(df)
| idx | colA | n | x1 | y1 | x2 | y2 | x3 | y3 | x4 | y4 |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 10 | 3 | 0 | 4 | 3 | 5 | 4.0 | 3.0 | NaN | NaN |
| 2 | 5 | 2 | 1 | 0 | 2 | 1 | NaN | NaN | NaN | NaN |
| 3 | 12 | 4 | 7 | 4 | 8 | 5 | 10.0 | 3.0 | 11.0 | 3.0 |
Desired Output
| idx | colA | counter | x | y |
|---|---|---|---|---|
| 1 | 10 | 1 | 0 | 4 |
| 1 | 10 | 2 | 3 | 5 |
| 1 | 10 | 3 | 4 | 3 |
| 2 | 5 | 1 | 1 | 0 |
| 2 | 5 | 2 | 2 | 1 |
| 3 | 12 | 1 | 7 | 4 |
| 3 | 12 | 2 | 8 | 5 |
| 3 | 12 | 3 | 10 | 3 |
| 3 | 12 | 4 | 11 | 3 |