简体   繁体   English

如何在函数内编写for循环以提取csv中的值?

[英]How do I write a for loop within a function to pickup values within a csv?

I have a file called sampleweather100 which has Latitudes and longtidudes of addresses. 我有一个名为sampleweather100的文件,其中包含地址的纬度和经度。 If i manually type in these lats and longs under the location list function, I get the output I desire. 如果我在位置列表功能下手动输入这些纬度和经度,则会得到我想要的输出。 However, I want to write a function where it pulls out the output for all rows of my csv without me manually entering it: 但是,我想编写一个函数,以在不手动输入的情况下为csv的所有行提取输出:

import pandas as pd
my_cities = pd.read_csv('sampleweather100.csv')
from wwo_hist import retrieve_hist_data

#lat = -31.967819
#lng = 115.87718
#location_list = ["-31.967819,115.87718"]

frequency=24
start_date = '11-JAN-2018'
end_date = '11-JAN-2019'
api_key = 'MyKey'

location_list = ["('sampleweather100.csv')['Lat'],('sampleweather100.csv')['Long']"]
hist_weather_data = retrieve_hist_data(api_key,
                                location_list,
                                start_date,
                                end_date,
                                frequency,
                                location_label = False,
                                export_csv = True,
                                store_df = True)

My function location_list = ["('sampleweather100.csv')['Lat'],('sampleweather100.csv')['Long']"] does not work. 我的函数location_list = ["('sampleweather100.csv')['Lat'],('sampleweather100.csv')['Long']"]不起作用。 Is there a better way or a forloop that will fetch each rows lat and long into that location_list function: 是否有更好的方法或forloop可以将经纬度较长的每一行提取到该location_list函数中:

Reprex of dataset: 数据集代表:

 my_cities
Out[89]: 
                City        Lat        Long
0          Lancaster  39.754545  -82.636371
1             Canton  40.851178  -81.470345
2             Edison  40.539561  -74.336307
3       East Walpole  42.160667  -71.213680
4             Dayton  39.270486 -119.577078
5    Fort Wainwright  64.825343 -147.673877
6            Crystal  45.056106  -93.350020
7            Medford  42.338916 -122.839771
8      Spring Valley  41.103816  -74.045399
9          Hillsdale  41.000879  -74.026089
10           Newyork  40.808582  -73.951553

Your way of building the list just does not make sense. 您建立列表的方式没有任何意义。 You are using the filename of the csv, which is just a string and holds no reference to the file itself or the dataframe you have created from it. 您正在使用csv的文件名,它只是一个字符串,不包含对文件本身或从中创建的数据框的引用。

Since you buildt a dataframe called my_cities from your csv using pandas , you need to extract your list of pairs from the dataframe my_cities : 由于使用pandas从csv中构建了一个名为my_cities的数据my_cities ,因此需要从my_cities数据my_cities提取成对列表:

location_list = [','.join([str(lat), str(lon)]) for lat, lon in zip(my_cities['Lat'], my_cities['Long'])]

This is the list you get with the above line using your sample dataframe: 这是使用示例数据框从上一行获得的列表:

['39.754545,-82.636371', '40.851178000000004,-81.470345',
 '40.539561,-74.33630699999999', '42.160667,-71.21368000000001',
 '39.270486,-119.577078', '64.825343,-147.673877', '45.056106,-93.35002',
 '42.338916,-122.839771', '41.103815999999995,-74.045399', 
 '41.000879,-74.026089', '40.808582,-73.951553']

You could use one of these to covert the dataframe into a list of comma-separated pairs: 您可以使用以下方法之一将数据帧转换为逗号分隔的对列表:

location_list = [
    '{},{}'.format(lat, lon) 
    for i, (lat, lon) in my_cities.iterrows()
]

or 要么

location_list = [
    '{},{}'.format(lat, lon) 
    for lat, lon in my_cities.values
]

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

相关问题 如何在 python 的 for 循环中正确编写 CSV 文件? - How do I properly write a CSV file within a for loop in python? 如何在 function 内的循环内返回值? - How do I return a value within a loop within a function? 如何比较python中csv文件的列中的值? - How do I compare values within a column in a csv file in python? 如何将打印的 for 循环值写入 csv 文件? - How do I write printed for loop values to a csv file? 如何创建在CSV文件中搜索的功能? - How do I create a function to search within a CSV file? 如何让我的 function 在我的 for 循环中返回多个值? (初学者) - how do i make my function returns multiple values within my for-loop? (Beginner) 如何使用循环 / lambda function 更改 df 列中的所有值(最简单的方法) - How do I change all my values within a df column using a loop / lambda function (the simplest way of doing so) 如何在同一个“for”循环中转到CSV文件的第一个记录? - How do I go to the first record of a CSV file within the same “for” loop? 如何在循环中检查准确性? - How do I check accuracy within the loop? 如何存储在 for 循环中生成的多个值以传递给 tkinter 按钮循环中的函数? - How do I store multiple values generated in a for loop to pass to a funtion within a tkinter button loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM