MaxB
MaxB

Reputation: 458

Determine reverse order of data given X/Y coordinates

Imagine an electrical connector. It has pins. Each pin has a corresponding X/Y location in space. I am trying to figure out how to mirror, or 'flip' each pin on the connector given their X/Y coordinate. note: I am using pandas version 23.4 Rough Paint Sketch Visual We can assume that x,y, and pin are not unique but connector is. Connectors can be any size, so two rows of 5, 3 rows of 6, etc.

x    y    pin   connector
1    1     A       1
2    1     B       1
3    1     C       1
1    2     D       1
2    2     E       1
3    2     F       1  
1    1     A       2
2    1     B       2
3    1     C       2
1    2     D       2
2    2     E       2
3    2     F       2

The dataframe column, 'flip', is the solution I am trying to get to. Notice the pins that would be in the same row are now in reverse order.

x    y    pin    flip   connector
1    1     A       C        1
2    1     B       B        1
3    1     C       A        1
1    2     D       F        1
2    2     E       E        1
3    2     F       D        1  
1    1     A       C        2
2    1     B       B        2
3    1     C       A        2
1    2     D       F        2
2    2     E       E        2
3    2     F       D        2

Upvotes: 3

Views: 450

Answers (3)

RootTwo
RootTwo

Reputation: 4418

import io
import pandas as pd

data = """
x    y    pin   connector
1    1     A       1
2    1     B       1
3    1     C       1
1    2     D       1
2    2     E       1
3    2     F       1
1    1     A       2
2    1     B       2
3    1     C       2
1    2     D       2
2    2     E       2
3    2     F       2
"""

#strip blank lines at the beginning and end
data = data.strip()

#make it quack like a file
data_file = io.StringIO(data)

#read data from a "wsv" file (whitespace separated values)
df = pd.read_csv(data_file, sep='\s+')

Make the new column:

flipped = []

for name, group in df.groupby(['connector','y']):
    flipped.extend(group.loc[::-1,'pin'])

df = df.assign(flip=flipped)

df

Final DataFrame:

    x   y   pin     connector   flip
0   1   1   A       1           C
1   2   1   B       1           B
2   3   1   C       1           A
3   1   2   D       1           F
4   2   2   E       1           E
5   3   2   F       1           D
6   1   1   A       2           C
7   2   1   B       2           B
8   3   1   C       2           A
9   1   2   D       2           F
10  2   2   E       2           E
11  3   2   F       2           D

Upvotes: 1

FLab
FLab

Reputation: 7506

You can create a map between the original coordinates and the coordinates of the 'flipped' component. Then you can select the flipped values.

import numpy as np

midpoint = 2

coordinates_of_flipped = pd.MultiIndex.from_arrays([df['x'].map(lambda x: x - midpoint  * np.sign(x - midpoint )), df['y'], df['connector']])

df['flipped'] = df.set_index(['x', 'y', 'connector']).loc[coordinates_of_flipped].reset_index()['pin']

which gives

Out[30]: 
    x  y pin  connector flipped
0   1  1   A          1       C
1   2  1   B          1       B
2   3  1   C          1       A
3   1  2   D          1       F
4   2  2   E          1       E
5   3  2   F          1       D
6   1  1   A          2       C
7   2  1   B          2       B
8   3  1   C          2       A
9   1  2   D          2       F
10  2  2   E          2       E
11  3  2   F          2       D

Upvotes: 0

Scott Boston
Scott Boston

Reputation: 153520

IIUC try using [::-1] a reversing element and groupby with transform:

df['flip'] = df.groupby(['connector','y'])['pin'].transform(lambda x: x[::-1])

Output:

    x  y pin  connector flip
0   1  1   A          1    C
1   2  1   B          1    B
2   3  1   C          1    A
3   1  2   D          1    F
4   2  2   E          1    E
5   3  2   F          1    D
6   1  1   A          2    C
7   2  1   B          2    B
8   3  1   C          2    A
9   1  2   D          2    F
10  2  2   E          2    E
11  3  2   F          2    D

Upvotes: 4

Related Questions