11  Funciones lambda

import pandas as pd
import matplotlib.pyplot as plt
f = "../../data/Temixco_2018_10Min.csv"
tmx = pd.read_csv(f,parse_dates=["time"])
tmx.time
0       2018-01-01 00:00:00
1       2018-01-01 00:10:00
2       2018-01-01 00:20:00
3       2018-01-01 00:30:00
4       2018-01-01 00:40:00
                ...        
52555   2018-12-31 23:10:00
52556   2018-12-31 23:20:00
52557   2018-12-31 23:30:00
52558   2018-12-31 23:40:00
52559   2018-12-31 23:50:00
Name: time, Length: 52560, dtype: datetime64[ns]
tmx["time"].apply(lambda x: x.year)
0        2018
1        2018
2        2018
3        2018
4        2018
         ... 
52555    2018
52556    2018
52557    2018
52558    2018
52559    2018
Name: time, Length: 52560, dtype: int64
tmx["year"] = tmx["time"].apply(lambda x: x.year)
tmx.columns
Index(['time', 'Ib', 'Ig', 'To', 'RH', 'WS', 'WD', 'P', 'year'], dtype='object')
%%timeit
tmx["P"].apply(lambda x: x*100)
23.1 ms ± 6.48 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)
%%timeit
tmx["P"]*100
101 μs ± 17.6 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
tmx['mensaje'] = tmx['To'].apply(lambda x: 'confort' if 20 <= x <= 24 else ('calor' if x > 24 else 'frío'))
tmx.mensaje
0        frío
1        frío
2        frío
3        frío
4        frío
         ... 
52555    frío
52556    frío
52557    frío
52558    frío
52559    frío
Name: mensaje, Length: 52560, dtype: object
tmx.mensaje.value_counts()
mensaje
calor      20907
confort    15921
frío       15732
Name: count, dtype: int64