如何在循环中增加熊猫行索引

时间:2019-05-18 01:04:33

标签: python pandas loops indexing foursquare

我试图增加一行的索引以获得Foursquare的URls,然后将其存储在同一Dataset的另一行中。 我知道这不是一项艰巨的任务,但我是新手,看不到我的错误。

我只会显示循环:

i=0
venue_id=df['id'][i] #I try to run the variable before and after the loop
for d in df['id']:
        #venue_id=df['id'][i]
        url = 'https://api.foursquare.com/v2/venues/{}?client_id={}&client_secret={}&v={}'.format(venue_id, CLIENT_ID, CLIENT_SECRET, VERSION)
        i=+1
        print(url)
        #venue_id=df['id'][+1] also try this but the result was always the same

结果

https://api.foursquare.com/v2/venues/557cda70498ec1b1d7c9bb38?client_id=WBVLIFMOB2B3KREKEPVECFKFZDJQJS2MCIXLERFTE4RZW0XA&client_secret=34TXWPRANTJP2E5QDBIKWEFTTPUKU3TOR2TR1I3GI0W4QY1X&v=20190101
https://api.foursquare.com/v2/venues/4d51750c7ee1a35d36e68f34?client_id=WBVLIFMOB2B3KREKEPVECFKFZDJQJS2MCIXLERFTE4RZW0XA&client_secret=34TXWPRANTJP2E5QDBIKWEFTTPUKU3TOR2TR1I3GI0W4QY1X&v=20190101
https://api.foursquare.com/v2/venues/4d51750c7ee1a35d36e68f34?client_id=WBVLIFMOB2B3KREKEPVECFKFZDJQJS2MCIXLERFTE4RZW0XA&client_secret=34TXWPRANTJP2E5QDBIKWEFTTPUKU3TOR2TR1I3GI0W4QY1X&v=20190101
https://api.foursquare.com/v2/venues/4d51750c7ee1a35d36e68f34?client_id=WBVLIFMOB2B3KREKEPVECFKFZDJQJS2MCIXLERFTE4RZW0XA&client_secret=34TXWPRANTJP2E5QDBIKWEFTTPUKU3TOR2TR1I3GI0W4QY1X&v=20190101

https://api.foursquare.com/v2/venues/之后的ID必须始终不同,而我仅获得第一个和第二个ID

我希望看到:

https://api.foursquare.com/v2/venues/557cda70498ec1b1d7c9bb38?...
https://api.foursquare.com/v2/venues/4d51750c7ee1a35d36e68f34?...
https://api.foursquare.com/v2/venues/4e4332422271bdbcf6659f9b?...
https://api.foursquare.com/v2/venues/4d63e05bdcafa1cd1e617611?...
etc..

1 个答案:

答案 0 :(得分:0)

我认为您可以做到

for x,d in df.iterrows():
        url = 'https://api.foursquare.com/v2/venues/{}?client_id={}&client_secret={}&v={}'.format(d.venue_id, d.CLIENT_ID, d.CLIENT_SECRET, d.VERSION)
        print(url)