1

I have a two dataframes. One with a coordinates of sites, the other with with connections between them. For example:

df1:

SITE_ID     LAT       LON
101       23.4244   31.5356
102       45.0090   14.2424
103       35.3444   19.4242
104       42.0000   18.0002

df2

SITE_ID  FIRST  SECOND  THIRD
101       102    104     NaN 
102       103    NaN     NaN
103       104    NaN     NaN
104       NaN    NaN     NaN

Now I wanna to change all value(in all columns) in df2 with coordinates from df1. Desired output:

 SITE_ID              FIRST               SECOND       THIRD
23.4244 31.5356    45.0090 14.2424   42.0000 18.0002   NaN 
45.0090 14.2424    35.3444 19.4242          NaN        NaN
35.3444 19.4242    42.0000 18.0002          NaN        NaN
42.0000 18.0002        NaN                  NaN        NaN

It's ok to have a little variation on this white comma or putting in lists. Later I'm gonna transform this to json and use it with javascript google maps api.. To be honest, I don't have so much idea, I've tried with transforming into dict, or overwrite while merging, but nothing really works till the end.

1 Answer 1

3

Here's one way, create mapping from df1 and replace values in df2

In [1120]: mapping = df1.set_index('SITE_ID').astype(str).apply(' '.join, axis=1).to_dict()

In [1121]: mapping
Out[1121]:
{101: '23.4244 31.5356',
 102: '45.009 14.2424',
 103: '35.3444 19.4242',
 104: '42.0 18.0002'}

In [1122]: df2.replace(mapping)
Out[1122]:
           SITE_ID            FIRST        SECOND  THIRD
0  23.4244 31.5356   45.009 14.2424  42.0 18.0002    NaN
1   45.009 14.2424  35.3444 19.4242           NaN    NaN
2  35.3444 19.4242     42.0 18.0002           NaN    NaN
3     42.0 18.0002              NaN           NaN    NaN
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.