I'm recently started with Python and I'm having trouble achieving the following:
I have a .CSV file with 3 columns (in total nearly 100.000 rows):
"date_time","temperature","precipitation"
"1957-12-31 19:00:00",6.9,0
"1957-12-31 20:00:00",6.6,0
"1957-12-31 21:00:00",6.8,5.5
"1957-12-31 22:00:00",7.2,0
I'm interested only in row [1] and [2] (the second and third row). If the third column of a row has a value > a certain value, I want the second column of that row printed / added to a list.
For example: On the fourth row the value 5.5 is bigger than 5, so I want the value 6.8 (the temperature) printed. At the end I'd like to have a list of all temperatures of hours with > 5mm rain.
(This way I can calculate if the average temperature is higher/lower when raining.)
My main problem is that Python sees the values not as numbers, so I can't say 'if precipitation > 5, print temperature'
Sorry if this question is already asked in another topic, I searched for this particular question for quite some time now and haven't found anything.)
with open("hourly_data_Maastricht.csv", 'r') as g:
file2 = csv.DictReader(g)
list = []
for j['precipitation'] in file2:
if float('precipitation') > 5:
list.append(float(j['temperature']))
ANSWER
Thank for all the comments, I've got my answer and a lot of usefull code that I will look into to be able to implement it in the future!!
Now i've got this:
with open("hourly_data_Maastricht.csv", 'r') as file:
reader = csv.DictReader(file)
list = []
for row in reader:
if float(row['precipitation']) > 5:
list.append(float(row['temperature']))
print(row)
print(list)
By using the print(row) and print(list) I could confirm that this code does what I need it to do.
Thanks a lot to everyone posting so quickly!