我正在使用带有python的googlemaps API,并且想读取一个包含LOCATIONID,LOCATION,X,Y,LAT,LNG,REF,ID的csv文件,其中每个都作为字典键
摘录自数据集
ChIJWzKeTARawokRjoqe_C9poOA,THEGIRLSOUTFITS,0,17,40.71380629999999,-74.0222972,dc1885d8c12ac669e9de3a73fedfb08c40deefd3,CmRSAAAA4Mvy3PVnmnYQKIZg-VX9-UrnrhlwOs7YFeY40gdXw1JfuUeEZndlhWxoIdI2K0nt1voCQDg2mqUVKV6EBgOKMTrHjDsMHOy7MJYBCUWguzLroifP1UMWTYSfUJD6E6sMEhB3psbJCGcDh5iS1PHyX4k5GhRBXigoOyADpURVG3NG2bIqXJ2i_g
ChIJDaBbDPlZwokRuHXkJGK9ER0,NYNJ State Line,0,29,40.7245721,-74.02168689999999,3d2cfc0a35b583760187c2369bb957b5a8cd9755,CmRRAAAAN25LD8IWAiu8CAIFM2eMS7eJVReV_UHzmR6sbaRkhDlJgiX-wK0Xlb85hC1qmDkarwQrGoM2u60zfSMr1MxcyzMAg3UDgL6nYwwu3WgG1aNlbpiqR2HlLFlK5XhimqI4EhDrfqXHOxKeztoT01Aicp3hGhSETo6Pj7gTk-dajA0RWco1djlrVA
ChIJQX6hfdlZwokRoCdPrD-8wSY,Shipyard Marina,0,59,40.751581,-74.021287,5f5e1aa75a39ce1880085d5094694a9ceda1ff43,CmRRAAAAp_VV6L2SQgnjC_TeMYRe_m2EYyjo_IdAOUqSeuRM9kbEMHSyguWco9jDV6rdYxk5s1E5s21BX0P7flWfK2LCegcKi1m_AOp2LuYtuzv0ql4-Jeq6-yx50Z180J9U4nrHEhAihnIm2i8MWwk7ATToiLBgGhR-w02BFnO9EGniCms-XWq00VrGOg
ChIJi3MPPNNZwokRDu8nE0ldrwk,Lincoln Harbor,0,68,40.759708,-74.02225,a3192a26d0a5a779da702a7f871612992e26f606,CmRRAAAAGcYQNoc-WgRCwh3rlBzeEmlG_XQgk_coHa4qUpzWU_9DiDWnU8eLjhInrZqpR7nKibXc0dcZIYiZEb-ehKvIrF6OfbLGrpmbf47YNaofAebceKirPZ_g6jMsc-_dcITaEhAKqkWagpXbgd3CcQf8Rtz-GhQtzlAGJlP7gOzV2KfXRGFIF2ZbAA
ChIJq35iOdNZwokRKvT9_KFP8jg,Lincoln Harbor,0,68,40.75984079999999,-74.0223211,38ea8de2bbf6d8a710bec3104d180d71b3d00735,CmRRAAAASNnGG2MHHIDGcXfgH-8iWUQOEahJdMZz6dy24azZrqyXqEtMb_yP1MJRGMV6Cp6lX05MgU2vNIfHcOGBfXiG0yWU7Qe0-t9_Uhm7JxNuAOEge9ZaCwNlrtxuxr8xSYsJEhAkL9vA29DV4nReD4e85D1TGhTbZYRfvNNRZi5a3NMUA0gyMYoR_A
ChIJ07hkOtNZwokRfY7FzKrxTkA,852858 Harbor Blvd,0,68,40.7599265,-74.02213569999999,29cfd2338edc10f94f5464b2fe152ab7bab3fca6,CmRbAAAAdlTDQx7qWhNnvgDQ_1afttKRpJ_MzBULrJLNX0TFEwpvKTjCvCZoXfV1F0A_diKH5D8qDjjucrjUC3gV8iVxqOYoHRGYwifE2FSiVycUrjunQxiDYAd-C6Q7QOqswAomEhAewuH-Q09vSAW_hNbQ0AUOGhQqRXCRrgxc6EAl2xRv1Z73M4MMug
ChIJnYuB1ixYwokR3w26K1mr3JQ,1500 Harbor Blvd,0,69,40.7609258,-74.02135559999999,ed57711af76a2534406a70f21d8a7119170b0f72,CmRbAAAAbPsHkeqeX3cx5MdED0Ao6ic0PE56xhGMdda0zXolIU7KsPmgPbT1CjXfyZ0p4ws_GYgdORZB3qP0idFvWBtMpdYpXCq0VknITsvfAoNfwYXoccYGsP7QvfpBPb1oTnywEhDwyrKX9JiQkYx9YaIXhkOwGhRCtFkL2futi7BkUxo_dMYEtsWVWA
ChIJk7Y29CxYwokRLcD4dsTPCCM,800816 Waterfront Terrace,0,71,40.7629756,-74.0223474,727cf2af87d28ec3a45f11e76e0746acfbec03b0,CmRbAAAAzgEUAIfexip_ePX2VQ29-iZMDPe-5RA4aGhFRoDkEGHrTaUON7ZGyn1r16erRSWIAhESFzycoG4Rwuw4OijSlLjKfK3_HCvC4fkrX-d6I7g7ffkIotwc1KK77UkHPMz-EhBoESHGx4Ke3H9y6XfDt9iaGhSGqEI5BmtpMu4URYwFmrT-2npHWQ
ChIJsZ2B5CxYwokRL8NrRMKxD9Q,Estuary Marketplace,0,71,40.7622809,-74.021577,1b6c2bd55b1bbabe3aac0d1eb2153e812c1026fd,CmRSAAAAO_HAcE9pNnA6r0FDp1eVsfS6jVn31DxU_JhcZNACGz9kfI5xOPjTbgM77JhJLxZPJqLgO_AnNXIIyEQV_wqO5Pr-YWJaIyhCEYA8Ene36VXgQ_90NMQwa_HNJJnWywkpEhBYQL6JbpsO0FJcOawnw5-2GhRE_QU6uh-cP-RKArGSyfEE2cxL3A
ChIJ64dzUNRZwokRIHBPnvi-eJs,Estuary Apartments,0,71,40.76240009999999,-74.0216088,8219ad9d883d8891a651bc35ff7bf7abe7c7b72a,CmRSAAAA7t8RT2MVUpaxNWBPItFU3pTQwNqXDuRY28GxZ_cywaLqOFIJ7taeXDHvGg19Y0MoIerm94HrD0iZQ_iIyoUrCKueeUETgHUGf8LQJMl9mqu784B5iIIXdf9-YrylAFJ9EhCvaowPZkGZvaEVLENy8fywGhRSEtZOm86qEyOCksoFWNJp9L_daw
ChIJWexh_9hZwokRgU6sf8pvbUg,Hoboken Weehawken Notary Public Dorian Cattani,0,71,40.762501,-74.021528,11be9c0505e8db6a7fcaea5fd4003d336b8667f2,CmRRAAAA9hteDDXN0TehPTxfnx6LxRjZTVHZOyOgbE1GJ42XOzB2v6htFgfliFz39e6llMHSlqvFS3uBiIKuH9bNu9qIfeSD8LlrSW5UXKP4U7sN-Zi1-IWH5QJw5S2hQCZHaNfeEhAT4Y9Q2hC0uG0vVi7CJPuVGhQPb5J4VpGisRfvRfiqEyPgTR8F5w
ChIJRaQT-CxYwokRlEhCGnjf6fs,EcoPure Home Cleaning Service Weehawken,0,72,40.76302460000001,-74.0218549,33800368abbf808a39da3e805c685b245e5baac4,CmRSAAAAyod24y6jY17fcd2b2mk6qgIoN_KWOCNxEN1zDniW9n7RHoWTm-MPXFN6N77XwYzORC-WarFmyP9jULhochuKcXcYP2y2ni7SWFviXVXOBtxvYVlHmfsyHctEvBy_GxKbEhB5ihU8my5kq4lQpQrwGIrQGhT-0cYHqRnMImt55xhU7CBDPPlA1w
ChIJFU-XRyxYwokRo5Do2LLO_mE,New Jersey 495,0,73,40.76429109999999,-74.0220365,23e5baf02ae51b7aeaf94efa56624c064bdbb456,CmRbAAAA_30BmNHIeCFJ7SVhLHWIbSa8llsfKQfYpVRC8X0feMrRlQ9ih9_vKtBfq-KRC6lcagEYQCBRdCfDob2divgzQEbrVkc9dR4v3oIfdyc5l9mlRyTnl1fOBxSeR8xMz78sEhD8FAIlIOza3aIMvqsdfjzrGhTj2R00Cv-lzmKIFEwLumKKd8g1cQ
我正在尝试的当前代码:
import pandas as pd
import numpy as np
import numpy.random as rdm
import matplotlib.pyplot as plt
import math as m
data = np.genfromtxt('PlacesFinalNew.csv', delimiter = ',')
size = int(data.size/8)
dict = {'LOCATIONID':[], 'LOCATION':[], 'X':[],'Y':[],'LAT':[],'LNG':[],'ID':[],'REF':[]}
for i in range(size):
dict['LOCATIONID'].append(data[i][0])
dict['LOCATION'].append(data[i][1])
dict['X'].append(data[i][2])
dict['Y'].append(data[i][3])
dict['LAT'].append(data[i][4])
dict['LNG'].append(data[i][5])
dict['ID'].append(data[i][6])
dict['REF'].append(data[i][7])
这适用于除“位置”以外的所有键。当我打印出dict ['LOCATION']时,我会得到一个nan列表。有人可以向我指出问题吗?
答案 0 :(得分:1)
您可以使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
:
zip
import csv
headers = ['LOCATIONID', 'LOCATION', 'X', 'Y', 'LAT', 'LNG', 'REF', 'ID']
with open('filename.csv') as f:
data = [dict(zip(headers, i)) for i in csv.reader(f)]
final_results = {i:[c[i] for c in data] for i in headers}
输出:
import json
print(json.dumps(final_results, indent=4))
答案 1 :(得分:0)
如果您的CSV文件包含标题名称,则只需读取csv即可创建熊猫,DataFrame对象会将Header转换为Dictionary键。
例如;-
df = pd.read_csv(fname)
df['ColumnName']
import pandas as pd
import numpy as np
import numpy.random as rdm
import matplotlib.pyplot as plt
import math as m
df = pd.read_csv('test.csv', delimiter = ',',na_values="nan")
print(df['LOCATIONID']) # Gives you Columns Data for 'LOCATIONID'
print(df['LOCATION'])
答案 2 :(得分:0)
您可以使用csv.DictReader()
将每一行读入字典:
import csv
fieldnames = ['LOCATIONID', 'LOCATION', 'X', 'Y', 'LAT', 'LNG', 'REF', 'ID']
with open('data.csv') as in_file:
csv_reader = csv.DictReader(in_file, fieldnames=fieldnames)
for row in csv_reader:
# print out row info
# e.g. row['LOCATION']
基本上,每一行都映射到collections.OrderedDict()
,这只是一个有序字典。
如果要将行信息映射到最终字典,则可以使用collections.defaultdict()
:
import csv
from collections import defaultdict
fieldnames = ['LOCATIONID', 'LOCATION', 'X', 'Y', 'LAT', 'LNG', 'REF', 'ID']
row_map = defaultdict(list)
with open('data.csv') as in_file:
csv_reader = csv.DictReader(in_file, fieldnames=fieldnames)
for row in csv_reader:
for field in row:
row_map[field].append(row[field])