2
I have a csv file that measures the temperature of every day of the year and I need to calculate the monthly average. I created a loop and even that worked but I wanted to know if there is a way to define a function so that the program is not too extensive, and also would like to know how to plot a curve graph that shows the average monthly temperatures.
My code:
import pandas as pd
dados = pd.read_csv('DadosClimaticos2018Londrina.csv',sep=';')
total = 0
t = 0
for i in range(len(dados)):
linha = dados.iloc[i]
data = linha['Data']
mes = data[3:5]
temperatura = linha['Temperatura']
if int(mes) == 1:
total1 = total + 1
t1 = ( t + temperatura )
tm1 = t1 / total1
if int(mes) == 2:
total2 = total + 1
t2 = ( t + temperatura )
tm2 = t2 / total2
if int(mes) == 3:
total3 = total + 1
t3 = ( t + temperatura )
tm3 = t3 / total3
if int(mes) == 4:
total4 = total + 1
t4 = ( t + temperatura )
tm4 = t4 / total4
if int(mes) == 5:
total5 = total + 1
t5 = ( t + temperatura )
tm5 = t5 / total5
if int(mes) == 6:
total6 = total + 1
t6 = ( t + temperatura )
tm6 = t6 / total6
if int(mes) == 7:
total7 = total + 1
t7 = ( t + temperatura )
tm7 = t7 / total7
if int(mes) == 8:
total8 = total + 1
t8 = ( t + temperatura )
tm8 = t8 / total8
if int(mes) == 9:
total9 = total + 1
t9 = ( t + temperatura )
tm9 = t9 / total9
if int(mes) == 10:
total10 = total + 1
t10 = ( t + temperatura )
tm10 = t10 / total10
if int(mes) == 11:
total11 = total + 1
t11 = ( t + temperatura )
tm11 = t11 / total11
if int(mes) == 12:
total12 = total + 1
t12 = ( t + temperatura )
tm12 = t12 / total12
Gigi, good morning! Can this calculation be done using functions that already exist in pandas? Hug!
– lmonferrari
ss, but which one? that’s what I don’t know
– user213102
If you are only calculating the average, the answer below meets you. I put the calculation inside a function and it returns a dataframe with these calculations. Hug!
– lmonferrari
Thank you, you’ve made the problem much easier
– user213102
If the answer is what you were looking for, consider marking as accepted. This helps people visualize that the question has already been solved and also encourages others to answer their questions. See how. Hug!
– lmonferrari