简体   繁体   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?

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)

Using pandas 使用熊猫

>>> 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.

相关问题 如何从CSV文件获取值以使用python加载到SQL数据库中? - How do I get to my values from a CSV file to load into my SQL database with python? 如何将打印的 for 循环值写入 csv 文件? - How do I write printed for loop values to a csv file? 如何在 python 的 for 循环中正确编写 CSV 文件? - How do I properly write a CSV file within a for loop in python? 在python中,如何将列表中的某些值仅写入CSV文件? - In python, how can I write only certain values from a list to a CSV file? 如何循环并将我的代码附加到 CSV 文件? - Python - How do I loop and append my code to a CSV file? - Python 如何让 python 从 output 我的代码中写入 csv 文件? - How do I get python to write a csv file from the output my code? 如何使用python写入.csv文件 - How do I write to a .csv file with python 我如何仅从 csv 文件中填充 python 中具有相同值的单元格? - how do i only populate cells that has the same values in python from a csv file? Python:如何将值从另一个csv文件写入一个csv文件 - Python: How to write values to a csv file from another csv file 在不使用熊猫的情况下,如何分析CSV数据并仅从CSV文件的某些列和行中提取某些值? - WITHOUT using Pandas, how do I analyze CSV data and only extract certain values from certain columns and rows of my CSV file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM