繁体   English   中英

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

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

我有一个名为sampleweather100的文件,其中包含地址的纬度和经度。 如果我在位置列表功能下手动输入这些纬度和经度,则会得到我想要的输出。 但是,我想编写一个函数,以在不手动输入的情况下为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)

我的函数location_list = ["('sampleweather100.csv')['Lat'],('sampleweather100.csv')['Long']"]不起作用。 是否有更好的方法或forloop可以将经纬度较长的每一行提取到该location_list函数中:

数据集代表:

 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

您建立列表的方式没有任何意义。 您正在使用csv的文件名,它只是一个字符串,不包含对文件本身或从中创建的数据框的引用。

由于使用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'])]

这是使用示例数据框从上一行获得的列表:

['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']

您可以使用以下方法之一将数据帧转换为逗号分隔的对列表:

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

要么

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

暂无
暂无

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

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