define average function with pandas

Asked

Viewed 145 times

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!

  • ss, but which one? that’s what I don’t know

  • 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!

  • Thank you, you’ve made the problem much easier

  • 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!

1 answer

1


Importing the pandas:

import pandas as pd

Reading the data and storing in a variable parse_dates converts the Date column to datetime64

dados = pd.read_csv('./DadosClimaticos2018Londrina.csv', sep = ';', parse_dates = ['Data'])

Creating the month column

dados['Mes'] = pd.DatetimeIndex(dados['Data'], yearfirst = True).month

Function that returns a data frame with average temperatures per month

def media_temperatura_mes(dataframe):
    return pd.DataFrame(dataframe.groupby('Mes')['Temperatura'].mean()).reset_index()

Calling the function and storing the result in a variable

temperaturas = media_temperatura_mes(dados)

Printing

temperaturas

   Mes  Temperatura
0   1   24.763441
1   2   23.852381
2   3   24.776344
3   4   24.008889
4   5   21.458065
5   6   22.020000
6   7   22.750538
7   8   21.627957
8   9   23.108889
9   10  23.232258
10  11  24.653333
11  12  26.073118

Example to plot the chart

import matplotlib.pyplot as plt

plt.plot(temperaturas['Mes'], temperaturas['Temperatura']) #define o que será plotado
plt.ylim(15) #define o limite mínimo do eixo y
plt.xticks(range(1,len(temperaturas['Mes']) + 1)) #adiciona de 1 até 12 ao eixo x
plt.xlabel('Mês') #adiciona label ao eixo x
plt.ylabel('Temperatura Média') #adiciona label ao eixo y
plt.show() # mostra o gráfico

Browser other questions tagged

You are not signed in. Login or sign up in order to post.