pythonpandasdata-analysischoroplethiso-3166

How to convert coordinates or addresses to ISO 3166 codes


while trying to analyse some data i ran into a problem where i am supposed to convert some locations to their iso3166 equivalence so as to make a need Choropleth map of the data but converting the data has seem to be a hassle sorry that i have not shown my trial solution.

here is the data set

        Location                                            Organisation
0   Cosmic Girl, Mojave Air and Space Port, Califo...   1
1   ELA-3, Guiana Space Centre, French Guiana, France   78
2   ELS, Guiana Space Centre, French Guiana, France 2
3   ELV-1 (SLV), Guiana Space Centre, French Guian...   15
4   First Launch Pad, Satish Dhawan Space Centre, ...   41
5   LA-Y1, Tanegashima Space Center, Japan  29
6   LA-Y2, Tanegashima Space Center, Japan  9
7   LC-2, Xichang Satellite Launch Center, China    35
8   LC-3, Xichang Satellite Launch Center, China    20
9   LC-39A, Kennedy Space Center, Florida, USA  120
10  LC-39B, Kennedy Space Center, Florida, USA  52
11  LC-7, Taiyuan Satellite Launch Center, China    21
12  LC-9, Taiyuan Satellite Launch Center, China    21
13  LP-0A, Wallops Flight Facility, Virginia, USA   13
14  LP-0B, Wallops Flight Facility, Virginia, USA   7
15  LP-1, Pacific Spaceport Complex, Alaska, USA    2
16  LP-41, Kauai, Pacific Missile Range Facility    1
17  Mu Pad, Uchinoura Space Center, Japan   2
18  NB-52B Carrier, Cape Canaveral AFS, Florida, USA    1
19  NB-52B Carrier, Edwards AFB, California, USA    5
20  Omelek Island, Ronald Reagan Ballistic Missile...   5
21  Rocket Lab LC-1A, M?hia Peninsula, New Zealand  13
22  SLC-20, Cape Canaveral AFS, Florida, USA    4
23  SLC-37B, Cape Canaveral AFS, Florida, USA   24
24  SLC-3E, Vandenberg AFB, California, USA 15
25  SLC-40, Cape Canaveral AFS, Florida, USA    60
26  SLC-41, Cape Canaveral AFS, Florida, USA    69
27  SLC-46, Cape Canaveral AFS, Florida, USA    1
28  SLC-4E, Vandenberg AFB, California, USA 15
29  SLC-4W, Vandenberg AFB, California, USA 35
30  SLC-576E, Vandenberg AFB, California, USA   10
31  SLC-6, Vandenberg AFB, California, USA  5
32  SLC-8, Vandenberg AFB, California, USA  7
33  Second Launch Pad, Satish Dhawan Space Centre,...   26
34  Site 109/95, Baikonur Cosmodrome, Kazakhstan    12
35  Site 110/37, Baikonur Cosmodrome, Kazakhstan    1
36  Site 133/3, Plesetsk Cosmodrome, Russia 31
37  Site 138 (LA-2B), Jiuquan Satellite Launch Cen...   14
38  Site 1S, Vostochny Cosmodrome, Russia   2
39  Site 200/39, Baikonur Cosmodrome, Kazakhstan    8
40  Site 250, Baikonur Cosmodrome, Kazakhstan   1
41  Site 31/6, Baikonur Cosmodrome, Kazakhstan  16
42  Site 370/13, Yasny Cosmodrome, Russia   10
43  Site 43/3, Plesetsk Cosmodrome, Russia  2
44  Site 43/4, Plesetsk Cosmodrome, Russia  9
45  Site 81/24, Baikonur Cosmodrome, Kazakhstan 8
46  Site 9401 (SLS-2), Jiuquan Satellite Launch Ce...   35
47  Site 95, Jiuquan Satellite Launch Center, China 9
48  Stargazer, Base Aerea de Gando, Gran Canaria    2
49  Stargazer, Cape Canaveral AFS, Florida, USA 6
50  Stargazer, Ronald Reagan Ballistic Missile Def...   4
51  Stargazer, Vandenberg AFB, California, USA  21
52  Stargazer, Wallops Flight Facility, Virginia, USA   5
53  Tai Rui Barge, Yellow Sea   1
54  Taiyuan Satellite Launch Center, China  2
55  Xichang Satellite Launch Center, China  1

I am hoping to get something like this below. its the first four rows of the above dataset

        Location      Organisation
0   USA             1
1   FRA             78
2   FRA                 15
3   FRA             1

though i noticed most of the addresses ended with their iso3166 representations i tried this


for data in df_data.Location:
    df_data.Location = df_data.Location[-1:]

i was aiming to get the last word which i hoped it would be the iso3166 which it was not and beside the code kept returning df_data.Location.values as "NAN"


Solution

  • Let's take a simplified version of the data you have:

    df = pd.DataFrame({
        'location': ['ELA-3, Guiana Space Centre, French Guiana, France',
                     'ELS, Guiana Space Centre, French Guiana, France', 
                     'LA-Y1, Tanegashima Space Center, Japan',
                     'LC-2, Xichang Satellite Launch Center, China'],
        'organization': [78, 2, 29, 35]
    })
    
    df
    

    Output:

    enter image description here

    Then:

    df["Country"] = df["location"].map(lambda x: x.split(",")[-1])
    df
    

    Output:

    enter image description here