Home > Blockchain >  add two values to the same column in python
add two values to the same column in python

Time:11-08

I want to add two value to the same column but I'm not sure if it's possible or not, i want actually to add only the city to the location column, but th problem is that i have a list called addresses and it contains the whole addresses and theyy are all differents (even the length) , i've used this code to add only the city but the problem is that sometimes the city is on the position len(addresses[i])-4 and sometimes not, so i wanted to add two value, i mean len(addresses[i])-4 and len(addresses[i])-5 to the location column, but is it possible ?

coordinates=[]
for i in range(10):
    coordinates.append(np.array([ [df_new['latitude'][i],df_new['longitude'][i]]]))

addresses = []

for coordinate in coordinates:
    addresses.append(geolocator.reverse(coordinate).address)
    
for i in range(10):
    addresses[i]=addresses[i].split(', ')
    df_new['location'][i]=addresses[i][len(addresses[i])-4]

this is an example of how it looks the list "addresses" like:

['2', 'ottstraße', 'Fechenheim', 'Ost', 'Frankfurt am Main', 'Hessen', '60386', 'Deutschland']
['7', 'Ring', 'Jungen', 'Ulm', 'Baden-Württemberg', '89066', 'Deutschland']
['Roland', 'Schön', 'Sandesneben', 'Lauenburg', 'Schleswig-Holstein', '25629', 'Deutschland']
['Zeiskliniken', '101', 'waldstraße', 'Yorckgebiet', 'Chemnitz', 'Sachsen', '99130', 'Deutschland']
['8', 'Nieksener Straße', 'Bad Oeynhausen', 'Kreis Minden-Lübbecke', 'Nordrhein-Westfalen', '32678', 'Deutschland']

CodePudding user response:

This will get you the cities only:

for coordinate in coordinates:
    addresses.append(geolocator.reverse(coordinate).raw['address']['city'])

Contrary to address, which provides the full address as a string, raw will get you a dictionary containing the available osm data, including the city.

  • Related