Ejemplo de clase sobre manipulacion de archivos csv usando Python
In [5]:
!head AportesDiarios_2004.csv
'head' is not recognized as an internal or external command,
operable program or batch file.
In [10]:
import pandas as pd
In [29]:
x=pd.read_csv('AportesDiario_2004.csv', sep=';', decimal=',', thousands='.', skiprows=2)
In [30]:
x
Out[30]:
Fecha
Region Hidrologica
Nombre Rio
Aportes Caudal m3/s
Aportes Energia kWh
Aportes %
0
01/01/2004
ANTIOQUIA
A. SAN LORENZO
17.56
3910000.0
72,27%
1
01/01/2004
ANTIOQUIA
CONCEPCION
6.15
1385300.0
123,69%
2
01/01/2004
ANTIOQUIA
DESV. EEPPM (NEC,PAJ,DOL)
11.43
2574700.0
147,13%
3
01/01/2004
ANTIOQUIA
GRANDE
18.65
4563500.0
79,23%
4
01/01/2004
ANTIOQUIA
GUADALUPE
11.28
2540900.0
80,15%
5
01/01/2004
ANTIOQUIA
GUATAPE
32.71
5405100.0
141,49%
6
01/01/2004
ANTIOQUIA
MIEL I
50.63
2214100.0
63,99%
7
01/01/2004
ANTIOQUIA
NARE
20.16
7057000.0
56,73%
8
01/01/2004
ANTIOQUIA
PORCE II
64.61
3242100.0
120,97%
9
01/01/2004
ANTIOQUIA
SAN CARLOS
12.80
1588700.0
70,92%
10
01/01/2004
ANTIOQUIA
TENCHE
1.81
407700.0
66,84%
11
01/01/2004
CARIBE
SINU URRA
152.23
1694100.0
86,88%
12
01/01/2004
CENTRO
BOGOTA N.R.
9.56
4152000.0
109,55%
13
01/01/2004
CENTRO
MAGDALENA BETANIA
194.95
2906000.0
68,54%
14
01/01/2004
CENTRO
PRADO
3.84
42100.0
8,10%
15
01/01/2004
ORIENTE
BATA
18.31
3013500.0
116,35%
16
01/01/2004
ORIENTE
CHUZA
1.96
851300.0
62,14%
17
01/01/2004
ORIENTE
GUAVIO
17.62
4231400.0
93,62%
18
01/01/2004
RIOS ESTIMADOS
OTROS RIOS (ESTIMADOS)
NaN
2755400.0
83,24%
19
01/01/2004
VALLE
ALTOANCHICAYA
29.75
3164200.0
72,41%
20
01/01/2004
VALLE
CALIMA
13.89
647800.0
124,58%
21
01/01/2004
VALLE
CAUCA SALVAJINA
122.69
2724000.0
73,42%
22
01/01/2004
VALLE
DIGUA
22.45
350800.0
85,56%
23
01/01/2004
VALLE
FLORIDA II
13.51
270200.0
207,85%
24
02/01/2004
ANTIOQUIA
A. SAN LORENZO
17.18
3825300.0
70,71%
25
02/01/2004
ANTIOQUIA
CONCEPCION
6.13
1380800.0
123,29%
26
02/01/2004
ANTIOQUIA
DESV. EEPPM (NEC,PAJ,DOL)
11.39
2565700.0
146,61%
27
02/01/2004
ANTIOQUIA
GRANDE
17.73
4338400.0
75,32%
28
02/01/2004
ANTIOQUIA
GUADALUPE
20.30
4572800.0
144,25%
29
02/01/2004
ANTIOQUIA
GUATAPE
18.00
2974400.0
77,86%
...
...
...
...
...
...
...
8754
30/12/2004
RIOS ESTIMADOS
OTROS RIOS (ESTIMADOS)
NaN
2604500.0
63,52%
8755
30/12/2004
VALLE
ALTOANCHICAYA
36.49
3859200.0
70,55%
8756
30/12/2004
VALLE
CALIMA
10.85
505900.0
69,30%
8757
30/12/2004
VALLE
CAUCA SALVAJINA
152.63
3381400.0
71,64%
8758
30/12/2004
VALLE
DIGUA
26.12
406600.0
71,33%
8759
30/12/2004
VALLE
FLORIDA II
8.23
164600.0
91,44%
8760
31/12/2004
ANTIOQUIA
A. SAN LORENZO
18.82
4112600.0
60,13%
8761
31/12/2004
ANTIOQUIA
CONCEPCION
5.16
1161900.0
77,46%
8762
31/12/2004
ANTIOQUIA
DESV. EEPPM (NEC,PAJ,DOL)
9.57
2154900.0
94,10%
8763
31/12/2004
ANTIOQUIA
GRANDE
23.37
5699700.0
71,25%
8764
31/12/2004
ANTIOQUIA
GUADALUPE
9.92
2233700.0
52,68%
8765
31/12/2004
ANTIOQUIA
GUATAPE
18.18
2939300.0
58,20%
8766
31/12/2004
ANTIOQUIA
MIEL I
76.67
3356700.0
72,66%
8767
31/12/2004
ANTIOQUIA
NARE
29.37
10178000.0
59,42%
8768
31/12/2004
ANTIOQUIA
PORCE II
74.85
3701300.0
105,15%
8769
31/12/2004
ANTIOQUIA
SAN CARLOS
14.26
1717800.0
51,28%
8770
31/12/2004
ANTIOQUIA
TENCHE
2.34
526900.0
61,99%
8771
31/12/2004
CARIBE
SINU URRA
163.31
1813900.0
56,68%
8772
31/12/2004
CENTRO
BOGOTA N.R.
3.81
1654700.0
16,97%
8773
31/12/2004
CENTRO
MAGDALENA BETANIA
253.18
3760000.0
67,75%
8774
31/12/2004
CENTRO
PRADO
30.68
329100.0
45,08%
8775
31/12/2004
ORIENTE
BATA
23.01
3783700.0
67,57%
8776
31/12/2004
ORIENTE
CHUZA
0.00
NaN
NaN
8777
31/12/2004
ORIENTE
GUAVIO
16.72
4013800.0
52,61%
8778
31/12/2004
RIOS ESTIMADOS
OTROS RIOS (ESTIMADOS)
NaN
2358400.0
57,52%
8779
31/12/2004
VALLE
ALTOANCHICAYA
23.50
2485400.0
45,44%
8780
31/12/2004
VALLE
CALIMA
9.16
427100.0
58,51%
8781
31/12/2004
VALLE
CAUCA SALVAJINA
151.79
3362800.0
71,25%
8782
31/12/2004
VALLE
DIGUA
20.79
323600.0
56,77%
8783
31/12/2004
VALLE
FLORIDA II
7.20
144000.0
80,00%
8784 rows × 6 columns
In [31]:
x['Fecha']
Out[31]:
0 01/01/2004
1 01/01/2004
2 01/01/2004
3 01/01/2004
4 01/01/2004
5 01/01/2004
6 01/01/2004
7 01/01/2004
8 01/01/2004
9 01/01/2004
10 01/01/2004
11 01/01/2004
12 01/01/2004
13 01/01/2004
14 01/01/2004
15 01/01/2004
16 01/01/2004
17 01/01/2004
18 01/01/2004
19 01/01/2004
20 01/01/2004
21 01/01/2004
22 01/01/2004
23 01/01/2004
24 02/01/2004
25 02/01/2004
26 02/01/2004
27 02/01/2004
28 02/01/2004
29 02/01/2004
...
8754 30/12/2004
8755 30/12/2004
8756 30/12/2004
8757 30/12/2004
8758 30/12/2004
8759 30/12/2004
8760 31/12/2004
8761 31/12/2004
8762 31/12/2004
8763 31/12/2004
8764 31/12/2004
8765 31/12/2004
8766 31/12/2004
8767 31/12/2004
8768 31/12/2004
8769 31/12/2004
8770 31/12/2004
8771 31/12/2004
8772 31/12/2004
8773 31/12/2004
8774 31/12/2004
8775 31/12/2004
8776 31/12/2004
8777 31/12/2004
8778 31/12/2004
8779 31/12/2004
8780 31/12/2004
8781 31/12/2004
8782 31/12/2004
8783 31/12/2004
Name: Fecha, dtype: object
In [33]:
x['Nombre Rio']
Out[33]:
0 A. SAN LORENZO
1 CONCEPCION
2 DESV. EEPPM (NEC,PAJ,DOL)
3 GRANDE
4 GUADALUPE
5 GUATAPE
6 MIEL I
7 NARE
8 PORCE II
9 SAN CARLOS
10 TENCHE
11 SINU URRA
12 BOGOTA N.R.
13 MAGDALENA BETANIA
14 PRADO
15 BATA
16 CHUZA
17 GUAVIO
18 OTROS RIOS (ESTIMADOS)
19 ALTOANCHICAYA
20 CALIMA
21 CAUCA SALVAJINA
22 DIGUA
23 FLORIDA II
24 A. SAN LORENZO
25 CONCEPCION
26 DESV. EEPPM (NEC,PAJ,DOL)
27 GRANDE
28 GUADALUPE
29 GUATAPE
...
8754 OTROS RIOS (ESTIMADOS)
8755 ALTOANCHICAYA
8756 CALIMA
8757 CAUCA SALVAJINA
8758 DIGUA
8759 FLORIDA II
8760 A. SAN LORENZO
8761 CONCEPCION
8762 DESV. EEPPM (NEC,PAJ,DOL)
8763 GRANDE
8764 GUADALUPE
8765 GUATAPE
8766 MIEL I
8767 NARE
8768 PORCE II
8769 SAN CARLOS
8770 TENCHE
8771 SINU URRA
8772 BOGOTA N.R.
8773 MAGDALENA BETANIA
8774 PRADO
8775 BATA
8776 CHUZA
8777 GUAVIO
8778 OTROS RIOS (ESTIMADOS)
8779 ALTOANCHICAYA
8780 CALIMA
8781 CAUCA SALVAJINA
8782 DIGUA
8783 FLORIDA II
Name: Nombre Rio, dtype: object
In [34]:
import statistics
In [40]:
x.head()
Out[40]:
Fecha
Region Hidrologica
Nombre Rio
Aportes Caudal m3/s
Aportes Energia kWh
Aportes %
0
01/01/2004
ANTIOQUIA
A. SAN LORENZO
17.56
3910000.0
72,27%
1
01/01/2004
ANTIOQUIA
CONCEPCION
6.15
1385300.0
123,69%
2
01/01/2004
ANTIOQUIA
DESV. EEPPM (NEC,PAJ,DOL)
11.43
2574700.0
147,13%
3
01/01/2004
ANTIOQUIA
GRANDE
18.65
4563500.0
79,23%
4
01/01/2004
ANTIOQUIA
GUADALUPE
11.28
2540900.0
80,15%
In [42]:
x[x['Region Hidrologica']=='ANTIOQUIA']
Out[42]:
Fecha
Region Hidrologica
Nombre Rio
Aportes Caudal m3/s
Aportes Energia kWh
Aportes %
0
01/01/2004
ANTIOQUIA
A. SAN LORENZO
17.56
3910000.0
72,27%
1
01/01/2004
ANTIOQUIA
CONCEPCION
6.15
1385300.0
123,69%
2
01/01/2004
ANTIOQUIA
DESV. EEPPM (NEC,PAJ,DOL)
11.43
2574700.0
147,13%
3
01/01/2004
ANTIOQUIA
GRANDE
18.65
4563500.0
79,23%
4
01/01/2004
ANTIOQUIA
GUADALUPE
11.28
2540900.0
80,15%
5
01/01/2004
ANTIOQUIA
GUATAPE
32.71
5405100.0
141,49%
6
01/01/2004
ANTIOQUIA
MIEL I
50.63
2214100.0
63,99%
7
01/01/2004
ANTIOQUIA
NARE
20.16
7057000.0
56,73%
8
01/01/2004
ANTIOQUIA
PORCE II
64.61
3242100.0
120,97%
9
01/01/2004
ANTIOQUIA
SAN CARLOS
12.80
1588700.0
70,92%
10
01/01/2004
ANTIOQUIA
TENCHE
1.81
407700.0
66,84%
24
02/01/2004
ANTIOQUIA
A. SAN LORENZO
17.18
3825300.0
70,71%
25
02/01/2004
ANTIOQUIA
CONCEPCION
6.13
1380800.0
123,29%
26
02/01/2004
ANTIOQUIA
DESV. EEPPM (NEC,PAJ,DOL)
11.39
2565700.0
146,61%
27
02/01/2004
ANTIOQUIA
GRANDE
17.73
4338400.0
75,32%
28
02/01/2004
ANTIOQUIA
GUADALUPE
20.30
4572800.0
144,25%
29
02/01/2004
ANTIOQUIA
GUATAPE
18.00
2974400.0
77,86%
30
02/01/2004
ANTIOQUIA
MIEL I
70.57
3086100.0
89,19%
31
02/01/2004
ANTIOQUIA
NARE
17.93
6276300.0
50,45%
32
02/01/2004
ANTIOQUIA
PORCE II
64.20
3221500.0
120,21%
33
02/01/2004
ANTIOQUIA
SAN CARLOS
11.84
1469500.0
65,60%
34
02/01/2004
ANTIOQUIA
TENCHE
3.52
792900.0
129,98%
48
03/01/2004
ANTIOQUIA
A. SAN LORENZO
17.00
3785300.0
69,97%
49
03/01/2004
ANTIOQUIA
CONCEPCION
6.17
1389800.0
124,09%
50
03/01/2004
ANTIOQUIA
DESV. EEPPM (NEC,PAJ,DOL)
11.46
2581500.0
147,51%
51
03/01/2004
ANTIOQUIA
GRANDE
17.48
4277200.0
74,26%
52
03/01/2004
ANTIOQUIA
GUADALUPE
6.10
1374100.0
43,35%
53
03/01/2004
ANTIOQUIA
GUATAPE
12.87
2126700.0
55,67%
54
03/01/2004
ANTIOQUIA
MIEL I
46.76
2044800.0
59,10%
55
03/01/2004
ANTIOQUIA
NARE
20.89
7312500.0
58,78%
...
...
...
...
...
...
...
8715
29/12/2004
ANTIOQUIA
GRANDE
26.05
6353300.0
79,42%
8716
29/12/2004
ANTIOQUIA
GUADALUPE
10.66
2400400.0
56,61%
8717
29/12/2004
ANTIOQUIA
GUATAPE
26.80
4332900.0
85,80%
8718
29/12/2004
ANTIOQUIA
MIEL I
82.77
3623700.0
78,44%
8719
29/12/2004
ANTIOQUIA
NARE
32.64
11311200.0
66,03%
8720
29/12/2004
ANTIOQUIA
PORCE II
85.86
4245700.0
120,62%
8721
29/12/2004
ANTIOQUIA
SAN CARLOS
14.65
1764800.0
52,68%
8722
29/12/2004
ANTIOQUIA
TENCHE
0.71
159900.0
18,81%
8736
30/12/2004
ANTIOQUIA
A. SAN LORENZO
19.09
4171600.0
60,99%
8737
30/12/2004
ANTIOQUIA
CONCEPCION
5.22
1175400.0
78,36%
8738
30/12/2004
ANTIOQUIA
DESV. EEPPM (NEC,PAJ,DOL)
9.69
2181900.0
95,28%
8739
30/12/2004
ANTIOQUIA
GRANDE
22.12
5394800.0
67,44%
8740
30/12/2004
ANTIOQUIA
GUADALUPE
10.72
2413900.0
56,93%
8741
30/12/2004
ANTIOQUIA
GUATAPE
26.67
4311900.0
85,38%
8742
30/12/2004
ANTIOQUIA
MIEL I
71.47
3129000.0
67,73%
8743
30/12/2004
ANTIOQUIA
NARE
24.47
8480000.0
49,50%
8744
30/12/2004
ANTIOQUIA
PORCE II
73.77
3647900.0
103,63%
8745
30/12/2004
ANTIOQUIA
SAN CARLOS
14.36
1729900.0
51,64%
8746
30/12/2004
ANTIOQUIA
TENCHE
0.72
162100.0
19,07%
8760
31/12/2004
ANTIOQUIA
A. SAN LORENZO
18.82
4112600.0
60,13%
8761
31/12/2004
ANTIOQUIA
CONCEPCION
5.16
1161900.0
77,46%
8762
31/12/2004
ANTIOQUIA
DESV. EEPPM (NEC,PAJ,DOL)
9.57
2154900.0
94,10%
8763
31/12/2004
ANTIOQUIA
GRANDE
23.37
5699700.0
71,25%
8764
31/12/2004
ANTIOQUIA
GUADALUPE
9.92
2233700.0
52,68%
8765
31/12/2004
ANTIOQUIA
GUATAPE
18.18
2939300.0
58,20%
8766
31/12/2004
ANTIOQUIA
MIEL I
76.67
3356700.0
72,66%
8767
31/12/2004
ANTIOQUIA
NARE
29.37
10178000.0
59,42%
8768
31/12/2004
ANTIOQUIA
PORCE II
74.85
3701300.0
105,15%
8769
31/12/2004
ANTIOQUIA
SAN CARLOS
14.26
1717800.0
51,28%
8770
31/12/2004
ANTIOQUIA
TENCHE
2.34
526900.0
61,99%
4026 rows × 6 columns
In [43]:
len(x[x['Region Hidrologica']=='ANTIOQUIA'])
Out[43]:
4026
In [44]:
x['Region Hidrologica']=='ANTIOQUIA'
Out[44]:
0 True
1 True
2 True
3 True
4 True
5 True
6 True
7 True
8 True
9 True
10 True
11 False
12 False
13 False
14 False
15 False
16 False
17 False
18 False
19 False
20 False
21 False
22 False
23 False
24 True
25 True
26 True
27 True
28 True
29 True
...
8754 False
8755 False
8756 False
8757 False
8758 False
8759 False
8760 True
8761 True
8762 True
8763 True
8764 True
8765 True
8766 True
8767 True
8768 True
8769 True
8770 True
8771 False
8772 False
8773 False
8774 False
8775 False
8776 False
8777 False
8778 False
8779 False
8780 False
8781 False
8782 False
8783 False
Name: Region Hidrologica, dtype: bool
In [47]:
seleccion=x['Region Hidrologica']=='ANTIOQUIA'
In [49]:
len(seleccion)
Out[49]:
8784
In [50]:
set(x['Nombre Rio'])
Out[50]:
{'A. SAN LORENZO',
'ALTOANCHICAYA',
'BATA',
'BOGOTA N.R.',
'CALIMA',
'CAUCA SALVAJINA',
'CHUZA',
'CONCEPCION',
'DESV. EEPPM (NEC,PAJ,DOL)',
'DIGUA',
'FLORIDA II',
'GRANDE',
'GUADALUPE',
'GUATAPE',
'GUAVIO',
'MAGDALENA BETANIA',
'MIEL I',
'NARE',
'OTROS RIOS (ESTIMADOS)',
'PORCE II',
'PRADO',
'SAN CARLOS',
'SINU URRA',
'TENCHE'}
In [51]:
x.groupby('Nombre Rio').mean()
Out[51]:
Aportes Caudal m3/s
Aportes Energia kWh
Nombre Rio
A. SAN LORENZO
36.466612
8.105400e+06
ALTOANCHICAYA
43.468716
4.620596e+06
BATA
95.478989
1.571243e+07
BOGOTA N.R.
28.051803
1.277674e+07
CALIMA
11.922077
5.560117e+05
CAUCA SALVAJINA
113.523306
2.519614e+06
CHUZA
9.742486
4.337967e+06
CONCEPCION
6.606667
1.488170e+06
DESV. EEPPM (NEC,PAJ,DOL)
8.312240
2.291866e+06
DIGUA
27.529536
4.299954e+05
FLORIDA II
10.712486
2.148367e+05
GRANDE
27.511967
6.729973e+06
GUADALUPE
20.167978
4.542911e+06
GUATAPE
34.103033
5.624479e+06
GUAVIO
85.884563
2.062469e+07
MAGDALENA BETANIA
374.490765
5.580630e+06
MIEL I
78.433251
3.430428e+06
NARE
49.711612
1.738808e+07
OTROS RIOS (ESTIMADOS)
NaN
3.700457e+06
PORCE II
99.292650
4.976887e+06
PRADO
45.597268
4.984191e+05
SAN CARLOS
24.910492
3.084509e+06
SINU URRA
284.444208
3.165074e+06
TENCHE
3.900874
8.786683e+05
In [54]:
n=set(x['Nombre Rio'])
n
Out[54]:
{'A. SAN LORENZO',
'ALTOANCHICAYA',
'BATA',
'BOGOTA N.R.',
'CALIMA',
'CAUCA SALVAJINA',
'CHUZA',
'CONCEPCION',
'DESV. EEPPM (NEC,PAJ,DOL)',
'DIGUA',
'FLORIDA II',
'GRANDE',
'GUADALUPE',
'GUATAPE',
'GUAVIO',
'MAGDALENA BETANIA',
'MIEL I',
'NARE',
'OTROS RIOS (ESTIMADOS)',
'PORCE II',
'PRADO',
'SAN CARLOS',
'SINU URRA',
'TENCHE'}
In [59]:
import statistics
In [69]:
for y in n:
z=x[x['Nombre Rio']==y]['Aportes Energia kWh']
if len(z) > 0:
print(y, statistics.mean(x[x['Nombre Rio']==y]['Aportes Energia kWh'].values[:]))
GRANDE 6729973.22404
MIEL I 3430428.4153
DESV. EEPPM (NEC,PAJ,DOL) nan
PRADO 498419.125683
GUAVIO 20624689.3443
PORCE II 4976887.15847
CALIMA 556011.748634
OTROS RIOS (ESTIMADOS) 3700456.8306
CONCEPCION 1488170.4918
CAUCA SALVAJINA 2519614.48087
NARE 17388083.6066
TENCHE 878668.306011
GUATAPE 5624479.23497
BOGOTA N.R. nan
FLORIDA II nan
SAN CARLOS 3084508.74317
MAGDALENA BETANIA 5580630.32787
ALTOANCHICAYA 4620595.90164
A. SAN LORENZO 8105399.72678
DIGUA 429995.355191
GUADALUPE 4542910.92896
CHUZA nan
BATA 15712428.4153
SINU URRA 3165074.04372
In [80]:
for y in n:
z=x[x['Nombre Rio']==y]['Aportes Energia kWh']
if len(z) > 0:
print(y, statistics.mean(z.values[:]))
GRANDE 6729973.22404
MIEL I 3430428.4153
DESV. EEPPM (NEC,PAJ,DOL) nan
PRADO 498419.125683
GUAVIO 20624689.3443
PORCE II 4976887.15847
CALIMA 556011.748634
OTROS RIOS (ESTIMADOS) 3700456.8306
CONCEPCION 1488170.4918
CAUCA SALVAJINA 2519614.48087
NARE 17388083.6066
TENCHE 878668.306011
GUATAPE 5624479.23497
BOGOTA N.R. nan
FLORIDA II nan
SAN CARLOS 3084508.74317
MAGDALENA BETANIA 5580630.32787
ALTOANCHICAYA 4620595.90164
A. SAN LORENZO 8105399.72678
DIGUA 429995.355191
GUADALUPE 4542910.92896
CHUZA nan
BATA 15712428.4153
SINU URRA 3165074.04372
In [76]:
Content source: diego-diaz/Diplomado2017
Similar notebooks: