Home > Mobile >  Set tuple pair of (x, y) coordinates into dict as key with id value
Set tuple pair of (x, y) coordinates into dict as key with id value

Time:04-27

The data looks like this:

d = {'location_id': [1, 2, 3, 4, 5], 'x': [47.43715, 48.213889, 46.631111, 46.551111, 47.356628], 'y': [11.880689, 14.274444, 14.371, 13.665556, 11.705181]}
df = pd.DataFrame(data=d)

print(df)
     location_id        x         y
0       1          47.43715   11.880689
1       2          48.213889  14.274444
2       3          46.631111  14.371
3       4          46.551111  13.665556
4       5          47.356628  11.705181

Expected output:

{(47.43715, 11.880689): 1, (48.213889, 14.274444): 2, (46.631111, 14.371): 3, ...}

So i can simply access ID providing point coordinates.

What i have tried:

dict(zip(df['x'].astype('float'), df['y'].astype('float'), zip(df['location_id'])))
Error: ValueError: dictionary update sequence element #0 has length 3; 2 is required

or

dict(zip(tuple(df['x'].astype('float'), df['y'].astype('float')), zip(df['location_id'])))
TypeError: tuple expected at most 1 arguments, got 2

I have Googled for it a while, but I am not very clear about it. Thank you for any assistance.

CodePudding user response:

I think this

result = dict(zip(zip(df['x'], df['y']), df['location_id']))

should give you what you want? Result:

{(47.43715, 11.880689): 1,
 (48.213889, 14.274444): 2,
 (46.631111, 14.371): 3,
 (46.551111, 13.665556): 4,
 (47.356628, 11.705181): 5}

CodePudding user response:

I didn't use a dataframe, is this what you wanted?

my_dict = {}
d = {'location_id': [1, 2, 3, 4, 5], 'x': [47.43715, 48.213889, 46.631111, 46.551111, 47.356628], 'y': [11.880689, 14.274444, 14.371, 13.665556, 11.705181]}


for i in range(len(d['location_id'])):
  my_dict[ (d['x'][i] , d['y'][i]) ] = d['location_id'][i]
  • Related