繁体   English   中英

如何在python中编写for循环以仅从csv文件中提取唯一值以加载到我的API脚本中?

[英]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?

我正在努力从https://www.worldweatheronline.com/developer/上获取天气API数据

我在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)

尽管这确实给了我输出,因为我手动输入了location_list,但我希望它为每个唯一的城市名称通读csv dataframes列。

我的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

我如何使其自动选取唯一的城市名称并将其用于location_list,而不是手动输入每个城市名称?

您可以这样操作,用您的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))

另外,如果您愿意安装熊猫:

import pandas as pd
my_cities = pd.read_csv('my_csv.csv')['city'].unique()
location_list = list(my_cities)

使用熊猫

>>> df = pandas.DataFrame([{'City': 'Foo'}, {'City': 'Bar'}, {'City': 'Foo'}])
>>> df['City'].unique()
array(['Foo', 'Bar'], dtype=object)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM