I searched for videos on youtube concerning working with latitude and longtitude for creating
interactive map.
I have a dataset on earthquakes,where the longtitude and latitude are both float:
Significant_Earthquakes_1900_2023
I tried to use this code unsuccessfully:
import csv
filename = '/kaggle/input/significant-earthquake-dataset-1900-2023/Significant Earthquake Dataset 1900-2023.csv'
keys = ('Latitude','Longitude')
records = []
with open(filename,'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
records.append({keys:row[key] for key
in keys})
records[0]
record = records[0]
coords = record['Latitude','Longitude'].split("(")[-1].split(")")[0]
coords
I got eventually this output:
{('Latitude', 'Longitude'): '132.0763'}
132.0763
How can I create visualization for plotly using geojson,csv or folium?
Thanks
Look if the following code solves your issue:
import csv
filename = 'Significant Earthquake Dataset 1900-2023.csv'
keys = ('Latitude','Longitude')
records = []
with open(filename,'r', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
records.append({key: row[key] for key in keys})
records
The output:
I downloaded the file from kagle. I put the encoding because it resulted error. And instead of keys inside the loop, I replaced by key.
you are storing the 'Latitude' and 'Longitude' keys as a tuple instead of separate key-value pairs. Additionally, you are only processing the first record in the dataset.
import csv
filename = '/kaggle/input/significant-earthquake-dataset-1900-2023/Significant Earthquake Dataset 1900-2023.csv'
records = []
with open(filename, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
lat = float(row['Latitude'].split('(')[-1].split(')')[0])
lon = float(row['Longitude'].split('(')[-1].split(')')[0])
records.append({'Latitude': lat, 'Longitude': lon})
print(records[0])
Related
I want to read the second column of data with the title nodes and assign to a variable with the same name for each point of t1.
import csv
with open('Data_10x10.csv', 'r') as f:
csv_reader = csv.reader(f)
The data looks like
csv_reader = csv.reader(f) is a Generator. So you can skip the headers by execute heading = next(csv_reader).
I would just use a dictionary data_t1 for storing node data with key name of column t1.
Try below one.
import csv
with open('Data_10x10.csv', 'r') as f:
data_t1={}
csv_reader = csv.reader(f)
# Skips the heading
heading = next(csv_reader)
for row in csv_reader:
data_t1[row[0]] = row[1]
Accessing data (key should be value of you t1 column, in this case '0', '1' etc.)
print(data_t1['0'])
print(data_t1['1'])
If you want to create dynamic variables with the same name for each point of t1 It is really bad idea. If your csv has lot of rows maybe millions, it will create millions of variables. So use dictionary with key and values.
i have this function
def audience(lat,lon,ids):
buff = buff_here(lat,lon)[-1]
count_visitas = []
for visitas in glob.glob(path): ......
df = pd.DataFrame(count_visitas, columns =['Visitas'])
df.to_csv(f'output/visitas_simi_{ids}.csv', index = False)
return count_visitas
I can't post the complete code here due to work issues, but it's works perfectly fine if i pass this parameters
audience(-33.51133739,-70.7558227,'CL0008')
Now, i have this csv and want to iterate over the rows of lat, lon and id as a parameter of the function. Any help, please? :c
You would need to bring the csv in with csv.DictReader and then you can call the desired columns:
csv_file = csv.DictReader(open(file, 'rU'))
for row in csv_file:
count_visitas = audience(row['lat'],row['lon'],row['ids'])
This code should work:
import csv
with open("names.csv", "r") as csv_file:
csv_reader = csv.DictReader(csv_file)
for line in csv_reader:
lat = line["first_name"]
lon = line["last_name"]
ids = line["email"]
audience(lat, lon, ids)
Im working on Path Planning of Drone using GPS co ordinates given in .CSV file , How to import GPS co ordintaes from .CSV file to my Python script directly??
taking the locations csv file having lattitute and longitde values as:
locations.csv
PFB piece of code:
import csv
filename = 'D:\Python\location.csv'
n=0
with open(filename, 'r') as csvfile:
csvreader = csv.reader(csvfile)
fields = next(csvreader)
for row in csvreader:
n = n + 1
print('location {} --> {}:{}\t{}:{}'.format(n,fields[0],row[0], fields[1],row[1]) )
Output:
location 1 --> Latitude:40.741895 Longitude:-73.989308
location 2 --> Latitude:41.741895 Longitude:-72.989308
location 3 --> Latitude:42.741895 Longitude:-71.989308
location 4 --> Latitude:43.741895 Longitude:-70.989308
location 5 --> Latitude:44.741895 Longitude:-74.989308
PFB sample code:
import csv
filename = 'D:\Python\location.csv'
rows = []
with open(filename, 'r') as csvfile:
csvreader = csv.reader(csvfile)
fields = next(csvreader)
print(fields)
for row in csvreader:
print(row)
rows.append(row)
print(rows)
output:
['Latitude', 'Longitude']
['40.741895', '-73.989308']
['41.741895', '-72.989308']
['42.741895', '-71.989308']
['43.741895', '-70.989308']
['44.741895', '-74.989308']
[['40.741895', '-73.989308'], ['41.741895', '-72.989308'], ['42.741895', '-71.989308'], ['43.741895', '-70.989308'], ['44.741895', '-74.989308']]
You may have a look at pandas in case you want to do more than merely iterate through the data: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html:
pd.read_csv('data.csv')
As a further extension there's also geopandas that is supposed to make working with geospatial data easier.
import csv
with open("somecities.csv") as f:
reader = csv.DictReader(f)
data = [r for r in reader]
Contents of somecities.csv:
Country,Capital,CountryPop,AreaSqKm
Canada,Ottawa,35151728,9984670
USA,Washington DC,323127513,9833520
Japan,Tokyo,126740000,377972
Luxembourg,Luxembourg City,576249,2586
New to python and I'm trying to read and append a csv file. I've spent some time experimenting with some responses to similar questions with no luck--which is why I believe the code above to be pretty useless.
What I am essentially trying to achieve is to store each row from the CSV in memory using a dictionary, with the country names as keys, and values being tuples containing the other information in the table in the sequence they are in within the CSV file.
And from there I am trying to add three more cities to the csv(Country, Capital, CountryPop, AreaSqKm) and view the updated csv. How should I go about doing all of this?
The desired additions to the updated csv are:
Brazil, Brasília, 211224219, 8358140
China, Beijing, 1403500365, 9388211
Belgium, Brussels, 11250000, 30528
EDIT:
Import csv
with open("somecities.csv", "r") as csvinput:
with open(" somecities_update.csv", "w") as csvresult:
writer = csv.writer(csvresult, lineterminator='\n')
reader = csv.reader(csvinput)
all = []
headers = next(reader)
for row in reader:
all.append(row)
# Now we write to the new file
writer.write(headers)
for record in all:
writer.write(record)
#row.append(Brazil, Brasília, 211224219, 8358140)
#row.append(China, Beijing, 1403500365, 9388211)
#row.append(Belgium, Brussels, 11250000, 30528)
So assuming you can use pandas for this I would go about it this way:
import pandas as pd
df1 = pd.read_csv('your_initial_file.csv', index_col='Country')
df2 = pd.read_csv('your_second_file.csv', index_col='Country')
dfs = [df1,df2]
final_df = pd.concat(dfs)
DictReader will only represent each row as a dictionary, eg:
{
"Country": "Canada",
...,
"AreaSqKm": "9984670"
}
If you want to store the whole CSV as a dictionary you'll have to create your own:
import csv
all_data = {}
with open("somecities.csv", "r") as f:
reader = csv.DictReader(f)
for row in reader:
# Key = country, values = tuple containing the rest of the data.
all_data[row["Country"]] = (row["Capital"], row["CountryPop"], row["AreaSqKm"])
# Add the new cities to the dictionary here...
# To write the data to a new CSV
with open("newcities.csv", "w") as f:
writer = csv.writer(f)
for key, values in all_data.items():
writer.writerow([key] + list(values))
As others have said, though, the pandas library could be a good choice. Check out its read_csv and to_csv functions.
Just another idea with creating and list and appending the new values through list construct as below, not tested:
import csv
with open("somecities.csv", "r") as csvinput:
with open("result.csv", "w") as csvresult:
writer = csv.writer(csvresult, lineterminator='\n')
reader = csv.reader(csvinput)
all = []
row = next(reader)
row.append(Brazil, Brasília, 211224219, 8358140)
row.append(China, Beijing, 1403500365, 9388211)
all.append(row)
for row in reader:
row.append(row[0])
all.append(row)
writer.writerows(all)
The simplest Form i see, tested in python 3.6
Opening a file with the 'a' parameter allows you to append to the end of the file instead of simply overwriting the existing content. Try that.
>>> with open("somecities.csv", "a") as fd:
... fd.write("Brazil, Brasília, 211224219, 8358140")
OR
#!/usr/bin/python3.6
import csv
fields=['Brazil', 'Brasília', '211224219','8358140']
with open(r'somecities.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow(fields)
I'm hoping you good folks can help with a project I'm working on. Essentially, I am trying to create a class that will take as an input a CSV file, examine the file for the number of columns of data, and store that data in key, value pairs in a dictionary. The code I have up to this point is below:
import csv
class DataStandard():
'''class to store and examine columnar data saved as a csv file'''
def __init__(self, file_name):
self.file_name = file_name
self.full_data_set = {}
with open(self.file_name) as f:
reader = csv.reader(f)
# get labels of each column in list format
self.col_labels = next(reader)
# find the number of columns of data in the file
self.number_of_cols = len(self.col_labels)
# initialize lists to store data using column label as key
for label in self.col_labels:
self.full_data_set[label] = []
The piece I am having a hard time with is once the dictionary (full_data_set) is created I'm not sure how to loop through the remainder of the CSV file and store the data in the respective values for each key (column). Everything I have tried until now hasn't worked because of how I have to loop through the csv.reader object.
I hope this question makes sense, but please feel free to ask any clarifying questions. Also, if you think of an approach that may work in a better more pythonic way I would appreciate the input. This is one of my first self-guided projects on class, so the subject is fairly new to me. Thanks in advance!
To read rows you can use for row in reader
data = []
with open('test.csv') as f:
reader = csv.reader(f)
headers = next(reader)
for row in reader:
d = dict(zip(headers, row))
#print(d)
data.append(d)
print('data:', data)
As said #PM2Ring csv has DictReader
with open('test.csv') as f:
reader = csv.DictReader(f)
data = list(reader)
print('data:', data)
This might give you ideas towards a solution. It is assumed that the labels are only on row 1, and the rest is data, and then the row length becomes 0 when there is no data:
import csv
class DataStandard():
'''class to store and examine columnar data saved as a csv file'''
def __init__(self, file_name):
self.file_name = file_name
self.full_data_set = {}
#modify method to the following:
with open(self.file_name) as f:
reader = csv.reader(f)
for row in reader:
if row = 0:
# get labels of each column in list format
self.col_labels = next(reader)
# find the number of columns of data in the file
self.number_of_cols = len(self.col_labels)
# initialize lists to store data using column label as key
for label in self.col_labels:
self.full_data_set[label] = []
else:
if len(row) != 0:
for i in range(self.number_of_cols):
label = self.col_labels[i]
self.full_data_set[label] = next(reader)
...My one concern is that while the 'with open(...)' is valid, some levels of indentation can be ignored, from my experience. In that case, to reduce the number of indentations, I would just separate 'row=0' and 'row!=0' operations into different instances of 'with open(...)' i.e. do row 1, close, open again, do row 2.