Pandas + Python: More efficient code
<p>This is my code:</p>

<pre><code>import pandas as pd
import os    
import glob as g


archivos = g.glob('C:\Users\Desktop\*.csv')

for archiv in archivos:
    nombre = os.path.splitext(archiv)[0] 
    df = pd.read_csv(archiv, sep=",")
    d = pd.to_datetime(df['DATA_LEITURA'], format="%Y%m%d")
    df['FECHA_LECTURA'] = d.dt.date
    del df['DATA_LEITURA']
    df['CONSUMO']=""
    df['DIAS']=""
    df["SUMDIAS"]=""
    df["SUMCONS"]=""
    df["CONSANUAL"] = ""            

    ordenado = df.sort_values(['NR_CPE','FECHA_LECTURA', 'HORA_LEITURA'], ascending=True)

    ##Agrupamos por el CPE
    agrupado = ordenado.groupby('NR_CPE')  

    for name, group in agrupado: #Recorremos el grupo    
          indice = group.index.values      
          inicio = indice[0] 
          fin = indice[-1]       
#Llenamos la primeras lectura de cada CPE, con esa lectura (porque no hay una lectura anterior)      
          ordenado.CONSUMO.loc[inicio] = 0
          ordenado.DIAS.loc[inicio] = 0
          cont=0
          for i in indice: #Recorremos lo que hay dentro de los grupos, dentro de los CPES(lecturas)
              if i &gt; inicio and i &lt;= fin :
                  cont=cont+1
                  consumo = ordenado.VALOR_LEITURA[indice[cont]] - ordenado.VALOR_LEITURA[indice[cont-1]]
                  dias = (ordenado.FECHA_LECTURA[indice[cont]] - ordenado.FECHA_LECTURA[indice[cont-1]]).days
                  ordenado.CONSUMO.loc[i] = consumo
                  ordenado.DIAS.loc[i] = dias   

# Hago las sumatorias, el resultado es un objeto DataFrame                 
    dias = agrupado['DIAS'].sum()  
    consu = agrupado['CONSUMO'].sum()    
    canu = (consu/dias) * 365  
#Contador con el numero de courrencias de los campos A,B y C
    conta=0
    contb=0
    contc=0  

#Como es un DF, para recorrerlo tengo que iterar sobre ellos para hacer la comparacion     
    print "Grupos:"    
    for ind, sumdias in dias.iteritems():
        if sumdias &lt;= 180:
            grupo = "A"
            conta=conta+1
        elif sumdias &gt; 180 and sumdias &lt;= 365:
            grupo = "B"
            contb=contb+1
        elif sumdias &gt; 365:
            grupo = "C"
            contc=contc+1


    print "grupo A: " , conta
    print "grupo B: " , contb
    print "grupo C: " , contc

    #Formateamos los campos para no mostrar todos los decimales    
    Fdias = dias.map('{:.0f}'.format)
    Fcanu = canu.map('{:.2f}'.format)

    frames = [Fdias, consu, Fcanu]
    concat = pd.concat(frames,axis=1).replace(['inf','nan'],[0,0])

    with open('C:\Users\Documents\RPE_PORTUGAL\Datos.csv','a') as f:
        concat.to_csv(f,header=False,columns=['CPE','DIAS','CONSUMO','CONSUMO_ANUAL'])

    try:         
        ordenado.to_excel(nombre+'.xls', columns=["NOME_DISTRITO",
        "NR_CPE","MARCA_EQUIPAMENTO","NR_EQUIPAMENTO","VALOR_LEITURA","REGISTADOR","TIPO_REGISTADOR",
        "TIPO_DADOS_RECOLHIDOS","FACTOR_MULTIPLICATIVO_FINAL","NR_DIGITOS_INTEIRO","UNIDADE_MEDIDA",
        "TIPO_LEITURA","MOTIVO_LEITURA","ESTADO_LEITURA","HORA_LEITURA","FECHA_LECTURA","CONSUMO","DIAS"],
        index=False) 
        print (archiv)        
        print ("===============================================")
        print ("*****Se ha creado el archivo correctamente*****")
        print ("===============================================")
    except IOError: 
        print ("===================================================")
        print ("Â¡Â¡Â¡Â¡Â¡Hubo un error en la escritura del archivo!!!!!")
        print ("===================================================")
</code></pre>

<p>This takes a file where I have lectures of energy consumption from different dates for every light meter(<code>'NR_CPE'</code>)  and do some calculations:</p>

<ol>
<li><p>Calculate the energy consumption for every <code>'NR_CPE'</code> by substracting the previous reading with the next one and the result put in a new column named <code>'CONSUMO'</code>.</p></li>
<li><p>Calculate the number of days where I'v got a reading and sum up the number of days</p></li>
<li>Add the consumption for every <code>'NR_CPE'</code> and calculate the anual consumption.</li>
<li>Finally I want to classify by number of days that every light meter(<code>'NR_CPE'</code>) has a lecture. A if it has less than 180 days, B between 180 and 1 year and C more than a year.  </li>
</ol>

<p>And finally write this result in two differents files.
Any idea of how should I re-code this to have the same output and be faster? 
Thank you all.</p>

<p>BTW this is my dataset:</p>

<pre><code>,NOME_DISTRITO,NR_CPE,MARCA_EQUIPAMENTO,NR_EQUIPAMENTO,VALOR_LEITURA,REGISTADOR,TIPO_REGISTADOR,TIPO_DADOS_RECOLHIDOS,FACTOR_MULTIPLICATIVO_FINAL,NR_DIGITOS_INTEIRO,UNIDADE_MEDIDA,TIPO_LEITURA,MOTIVO_LEITURA,ESTADO_LEITURA,DATA_LEITURA,HORA_LEITURA
0,GUARDA,A002000642VW,101,1865411,4834,001,S,1,1,4,kWh,1,1,A,20150629,205600
1,GUARDA,A002000642VW,101,1865411,4834,001,S,1,1,4,kWh,2,2,A,20160218,123300
2,GUARDA,A002000642VJ,122,204534,25083,001,S,1,1,5,kWh,1,1,A,20150629,205700
3,GUARDA,A002000642VJ,122,204534,27536,001,S,1,1,5,kWh,2,2,A,20160218,123200
4,GUARDA,A002000642HR,101,1383899,11734,001,S,1,1,5,kWh,1,1,A,20150629,205600
5,GUARDA,A002000642HR,101,1383899,11800,001,S,1,1,5,kWh,2,2,A,20160218,123000
6,GUARDA,A002000995VM,101,97706436,12158,001,S,1,1,5,kWh,1,3,A,20150713,155300
7,GUARDA,A002000995VM,101,97706436,12163,001,S,1,1,5,kWh,2,2,A,20160129,162300
8,GUARDA,A002000995VM,101,97706436,12163,001,S,1,1,5,kWh,2,2,A,20160202,195800
9,GUARDA,A2000995VM,101,97706436,12163,001,S,1,1,5,kWh,1,3,A,20160404,145200
10,GUARDA,A002000996LV,168,5011703276,3567,001,V,1,1,6,kWh,1,1,A,20150528,205900
11,GUARDA,A02000996LV,168,5011703276,3697,001,V,1,1,6,kWh,2,2,A,20150929,163500
12,GUARDA,A02000996LV,168,5011703276,1287,002,P,1,1,6,kWh,1,1,A,20150528,205900
</code></pre>

<p>Generally you want to <a href="http://stackoverflow.com/a/7837947/12663">avoid for loops in pandas</a>.
For example, the first loop where you calculate total consumption and days could be rewritten as a groupby apply something like:</p>

<pre><code>def last_minus_first(df):
    columns_of_interest = df[['VALOR_LEITURA', 'days']]
    diff = columns_of_interest.iloc[-1] - columns_of_interest.iloc[0]
    return diff
df['date'] = pd.to_datetime(df['DATA_LEITURA'], format="%Y%m%d")
df['days'] = (df['date'] - pd.datetime(1970,1,1)).dt.days # create days column
df.groupby('NR_CPE').apply(last_minus_first)
</code></pre>

<p>(btw I don't understand why you are subtracting each entry from the previous, surely for meter readings this is the same as last-first?)</p>

<p>Then given the result of the above as consumption, you can replace your second for loop (for ind, sumdias in dias.iteritems()) with something like:</p>

<pre><code>pd.cut(consumption.days, [-1, 180, 365, np.inf], labels=['a', 'b', 'c']).value_counts()
</code></pre>

python pandas