Python: CSV - print value of column if other column is big enough

Viewed 1209

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!

4 Answers

You don't really need a list to calculate that, you can just filter the dataframe and take the average of the temperature column.

import pandas as pd

df = pd.read_csv("hourly_data_Maastricht.csv")
df['precipitation'] = pd.to_numeric(df['precipitation'])
df_filtered = df[[df['precipitation'] > 5]]
avg_temp = df_filtered['temperature'].mean()

In case you don't want to include a whole new framework (pandas) for that, you actually already have most of the solution, you just didn't see one thing:

You are using csv.DictReader - it creates a dict according to the CSV header! So you just do:

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

i.e., you iterate over the rows in the file and you access the columns in the dict with the column name as key.

Also see the documentation (Notes: this is for Python3, you didn't specify which version).

'Pandas is an open source library which allow you to manipulate .csv, .txt and other kind of files. In your case you should do the following:

import pandas as pd

df = pd.read_csv('hourly_data_Maastricht.csv') #Now df (short for dataframe) you can manipulate your table.
result = df[df['precipitation'] > 5] 
print(result)

So, you first create your dataframe, then you filter the value you want, for example when you write this "result = df[df['precipitation'] > 5]" you are saying the following: from the dataframe, which has a column called 'precitipation' give me all the values higher than 5, and then you print your result. If you want to calculate something from result, from example mean, std, range, no problem, just call the specific method for that. Hope this help you!

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!

Related