[英]How do I write a for loop in python to pick up only unique values from a csv file to load into my API script?
I am working on fetching weather API data from https://www.worldweatheronline.com/developer/ 我正在努力从https://www.worldweatheronline.com/developer/上获取天气API数据
My script in python: 我在python中的脚本:
from wwo_hist import retrieve_hist_data
frequency=12
start_date = '11-JAN-2018'
end_date = '11-JAN-2019'
api_key = 'MYKEY'
location_list = [ "Lancaster","Canton"]
hist_weather_data = retrieve_hist_data(api_key,
location_list,
start_date,
end_date,
frequency,
location_label = False,
export_csv = True,
store_df = True)
While this does give me my output because I manually type in my location_list, I want it to read through my csv dataframes column for every unique city name. 尽管这确实给了我输出,因为我手动输入了location_list,但我希望它为每个唯一的城市名称通读csv dataframes列。
My csv dataframe reprex is as follows(used R's unique function): 我的csv数据框代表如下(使用R的唯一功能):
unique(MyData$City)
[1] Lancaster Canton Edison
[4] East Walpole Dayton Fort Wainwright
[7] Crystal Medford Spring Valley
[10] Hillsdale Newyork Butte
[13] Alameda Monroe Astoria
[16] Austin Cortlandt Manor Central Square
[19] Redding Morgantown Tyngsboro
[22] Peabody Wenonah Milford
[25] Groton Springfield Palermo
[28] Helotes Conroe Somerset
[31] Clifton Park Aberdeen Palm Springs
[34] Gilbert Hopkinton San Diego
[37] Detroit Carrollton Calabasas
[40] Parker Pleasant Hill San Jose
How can i automate it to pick up the unique city names and use it for location_list instead of manually entering each city name? 我如何使其自动选取唯一的城市名称并将其用于location_list,而不是手动输入每个城市名称?
You could do it like this, substituting my_csv.csv
with your csv file, and 'city'
with the name of the column in the csv file where your city data is. 您可以这样操作,用您的csv文件替换
my_csv.csv
,并用您城市数据所在的csv文件中列的名称替换'city'
。
import csv
my_cities = []
with open('my_csv.csv', newline='') as csvfile:
my_data = csv.reader(csvfile, delimiter=',')
for index, row in enumerate(my_data):
if index == 0:
my_column_index = row.index('city')
if index != 0 and len(row) > 0:
my_cities.append(row[my_column_index])
location_list = list(set(my_cities))
Alternatively, if you're willing to install Pandas: 另外,如果您愿意安装熊猫:
import pandas as pd
my_cities = pd.read_csv('my_csv.csv')['city'].unique()
location_list = list(my_cities)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.