0

How to add addition column from same row in iterate specific column

join-geo.csv file

title,place
Battle of Lade,Miletus
Battle of Mursa Major,Pannonia
Battle of Mursa Major,Osijek

Here is my code

df = pd.read_csv('join-geo.csv')

with open('search_geo---' + str(timestr) + '.csv', 'w', encoding='utf-8') as output:
    fieldnames = ['title','place', 'geoname']
    output_writer = csv.DictWriter(output, delimiter=',', fieldnames=fieldnames)
    output_writer.writeheader()

    for i in df.place:
        params = {'username': "xxx",
                  'name_equals': i,
                  'featureClass': ['P', 'A'],
                  'maxRows': "1"}

        e = requests.get(url, params=params)
        if e:
            pretty_json = json.loads(e.text)
        else:
            print(f"{i} is unknown")
        for item in pretty_json["geonames"]:
            output_writer.writerow({'place': item['name'], 'geoname': "https://www.geonames.org/{}".format(item['geonameId'])})

it just print something like this

place,geoname
Miletus,https://www.geonames.org/4814762
Pannonia (Roman province),https://www.geonames.org/8181615
Osijek,https://www.geonames.org/3193935

What I want is it can print the title column too with the same iteration from df.place

and I've tried by adding this output_writer.writerow({'title': (a for a in df.title),'place': item['name'], 'geoname': "https://www.geonames.org/{}".format(item['geonameId'])}) but it turns out not in the right path/row

expected output:

title,place,geoname
Battle of Lade,Miletus,https://www.geonames.org/4814762
Battle of Mursa Major,Pannonia (Roman province),https://www.geonames.org/8181615
Battle of Mursa Major,Osijek,https://www.geonames.org/3193935
drowsyone
  • 27
  • 6

0 Answers0