Function parameters iterate over a csv file values - python

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)

Related

Extracting longtitude and latitude for plotly interactive map

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])

csv module: ordered dictionary manipulation?

I have a csv with two fields, 'positive' and 'negative'. I am trying to add the positive words to a list from the csv using the DictReader() module. Here is the following code.
import csv
with open('pos_neg_cleaned.csv', 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
positive_list = []
for n in csv_reader:
if n == 'positive' and csv_reader[n] != None :
positive_list.append(csv_reader[n])
However the program returns an empty list. Any idea how to get around this issue? Or what am I doing wrong?
That's because you can only read once from the csv_reader generator. In this case your do this with the print statement.
With a little re-arranging it should work fine:
import csv
with open('pos_neg_cleaned.csv', 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
positive_list = []
for n in csv_reader:
# put your print statement inside of the generator loop.
# otherwise the generator will be empty by the time your run the logic.
print(n)
# as n is a dict, you want to grab the right value from that dict.
# if it contains a value, then do something with it.
if n['positive']:
# Here you want to call the value from your dict.
# Don't try to call the csv_reader - but use the given data.
positive_list.append(n['positive'])
Every row in DictReader is a dictionary, so you can retrieve "columns values" using column name as "key" like this:
positive_column_values = []
for row in csv_dict_reader:
positive_column_value = row["positive"]
positive_column_values.append(positive_column_value)
After execution of this code, "positive_column_values" will have all values from "positive" column.
You can replace this code with your code to get desired result:
import csv
with open('pos_neg_cleaned.csv', 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
positive_list = []
for row in csv_reader:
positive_list.append(row["positive"])
print(positive_list)
Here's a short way with a list comprehension. It assumes there is a header called header that holds (either) positive or negative values.
import csv
with open('pos_neg_cleaned.csv', 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
positive_list = [line for line in csv_reader if line.get('header') == 'positive']
print(positive_list)
alternatively if your csv's header is positive:
positive_list = [line for line in csv_reader if line.get('positive')]

Python output to csv file

I have a question in output to a csv file in Python:
Code as below:
import numpy as np
import scipy.stats as stats
from scipy.stats import poisson, norm
# Read the csv file and obtain corresponding parameter mu, cs and co.
import csv
with open('r1.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
print row
mu = row[0]
cs = row[1]
co = row[2]
mu = float(mu)
cs = float(cs)
co = float(co)
# Generate a Poisson Distribution and
G = poisson(mu)
p = G.pmf(np.arange(3*mu))
# Define Z(Q) for the total cost estimation
def Z(Q):
ES = sum(i*p[i] for i in range(len(p)))
return cs*max((Q-ES), 0) + co*max((ES-Q), 0)
# Obtain Qstar
Qstar = np.ceil(poisson.ppf(co/(cs+co), mu))-1
Qstar = int(np.float64(Qstar).item())
This part of code works fine for me and I got Qstar = 5 in this simple example. How could I output it to a csv file?
Order_Number
5
I have the following code to call Qstar:
with open('test.csv', 'wb') as fp:
a = csv.writer(fp, delimiter=',')
data = [['Order_Number'],['Qstar']]
a.writerows(data)
But it seems I only obtain
Order_Number
Qstar
The nhow could I call 'Qstar' correctly?
Thank you!
Try this:
with open('test.csv', 'wb') as fp:
a = csv.writer(fp, delimiter=',')
data = [['Order_Number'],[Qstar]]
a.writerows(data)
By using single quotes around Qstar you are telling Python to create a string with the value 'Qstar'. In order to output the value of the variable Qstar, just don't use the single quotes:
with open('test.csv', 'wb') as fp:
a = csv.writer(fp, delimiter=',')
data = [['Order_Number'],[Qstar]]
a.writerows(data)
That's because you feed data array with strings instead of numbers. Remove the single quotes from Qstar and Order_number.

Error while reading csv file using python

I am trying to read a specific comma value from a csv file but i am getting the full row value how can i get the specific comma value
My csv looks like this
Index,Time,Energy
1,1.0,45.034
i need to get the values of Energy in each column.
import csv
with open('somefile.csv') as f:
reader = csv.DictReader(f, delimiter=',')
rows = list(reader)
for row in rows:
print(row['Energy'])
f = open('file.txt')
f.readline() # To skip header
for line in f:
print(line.split(',')[2])
f.close()
If you want it working even if the position of column Energy changes, you can do this:
with open('your_file.csv') as f:
# Get header
header = f.readline()
energy_index = header.index('Energy')
# Get Energy value
for line in f.readlines():
energy = line.split(',')[energy_index]
# do whatever you want to do with Energy
Check the below code. Hoping this is what you are looking for.
import csv
try:
fobj = open(file_name, 'r')
file_content = csv.reader(fobj, delimiter=',', quotechar='|')
except:
fobj.close()
file_content = False
if file_content:
for row_data in file_content:
try:
# This will return the 3rd columns value i.e 'energy'
row_data[2] = row_data[2].strip()
print row_data[2]
except:
pass
fobj.close()

How to read one single line of csv data in Python?

There is a lot of examples of reading csv data using python, like this one:
import csv
with open('some.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
print(row)
I only want to read one line of data and enter it into various variables. How do I do that? I've looked everywhere for a working example.
My code only retrieves the value for i, and none of the other values
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in reader:
i = int(row[0])
a1 = int(row[1])
b1 = int(row[2])
c1 = int(row[2])
x1 = int(row[2])
y1 = int(row[2])
z1 = int(row[2])
To read only the first row of the csv file use next() on the reader object.
with open('some.csv', newline='') as f:
reader = csv.reader(f)
row1 = next(reader) # gets the first line
# now do something here
# if first row is the header, then you can do one more next() to get the next row:
# row2 = next(f)
or :
with open('some.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
# do something here with `row`
break
you could get just the first row like:
with open('some.csv', newline='') as f:
csv_reader = csv.reader(f)
csv_headings = next(csv_reader)
first_line = next(csv_reader)
You can use Pandas library to read the first few lines from the huge dataset.
import pandas as pd
data = pd.read_csv("names.csv", nrows=1)
You can mention the number of lines to be read in the nrows parameter.
Just for reference, a for loop can be used after getting the first row to get the rest of the file:
with open('file.csv', newline='') as f:
reader = csv.reader(f)
row1 = next(reader) # gets the first line
for row in reader:
print(row) # prints rows 2 and onward
From the Python documentation:
And while the module doesn’t directly support parsing strings, it can easily be done:
import csv
for row in csv.reader(['one,two,three']):
print row
Just drop your string data into a singleton list.
The simple way to get any row in csv file
import csv
csvfile = open('some.csv','rb')
csvFileArray = []
for row in csv.reader(csvfile, delimiter = '.'):
csvFileArray.append(row)
print(csvFileArray[0])
To print a range of line, in this case from line 4 to 7
import csv
with open('california_housing_test.csv') as csv_file:
data = csv.reader(csv_file)
for row in list(data)[4:7]:
print(row)
I think the simplest way is the best way, and in this case (and in most others) is one without using external libraries (pandas) or modules (csv). So, here is the simple answer.
""" no need to give any mode, keep it simple """
with open('some.csv') as f:
""" store in a variable to be used later """
my_line = f.nextline()
""" do what you like with 'my_line' now """

Categories