我有一个名为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
答案 0 :(得分:0)
您建立列表的方式没有任何意义。您正在使用csv的文件名,它只是一个字符串,不包含对文件本身或从中创建的数据框的引用。
由于您使用my_cities
从csv构建了一个名为pandas
的数据框,因此需要从数据框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']
答案 1 :(得分:0)
您可以使用其中之一将数据帧隐藏到以逗号分隔的对列表中:
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
]