Latihan Python
Latihan Python
http://localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 1/15
10/2/2018 Untitled1
Out[4]:
EST Temperature DewPoint Humidity Sea Level PressureIn VisibilityMiles WindSpeedMPH PrecipitationIn CloudCover Events WindDirDegrees Unnamed: 11
In [5]: df['Temperature'].max()
Out[5]: 50
In [6]: df['EST'][df['Events']=='Rain']
Out[6]: 8 01-09-16
9 01-10-16
15 1/16/2016
26 1/27/2016
Name: EST, dtype: object
http://localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 2/15
10/2/2018 Untitled1
In [7]: df.head()
Out[7]:
EST Temperature DewPoint Humidity Sea Level PressureIn VisibilityMiles WindSpeedMPH PrecipitationIn CloudCover Events WindDirDegrees Unnamed: 11
In [8]: df.tail()
Out[8]:
EST Temperature DewPoint Humidity Sea Level PressureIn VisibilityMiles WindSpeedMPH PrecipitationIn CloudCover Events WindDirDegrees Unnamed: 11
In [10]: Temp
Out[10]: 0 38
1 36
2 40
3 25
4 20
5 33
6 39
7 39
8 44
9 50
10 33
11 35
12 26
13 30
14 43
15 47
16 36
17 25
18 22
19 32
20 31
21 26
22 26
23 28
24 34
25 43
26 41
27 37
28 36
29 34
30 46
Name: Temperature, dtype: int64
http://localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 3/15
10/2/2018 Untitled1
Out[11]:
Temperature DewPoint Humidity Sea Level PressureIn VisibilityMiles WindSpeedMPH PrecipitationIn CloudCover Events WindDirDegrees Unnamed: 11
EST
http://localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 4/15
10/2/2018 Untitled1
In [12]: Temp
Out[12]: 0 38
1 36
2 40
3 25
4 20
5 33
6 39
7 39
8 44
9 50
10 33
11 35
12 26
13 30
14 43
15 47
16 36
17 25
18 22
19 32
20 31
21 26
22 26
23 28
24 34
25 43
26 41
27 37
28 36
29 34
30 46
Name: Temperature, dtype: int64
In [13]: temp=df['EST']df['Temperature']
temp
http://localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 5/15
10/2/2018 Untitled1
In [14]: temp=df['EST'][df['Temperature']]
temp
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3062 try:
-> 3063 return self._engine.get_loc(key)
3064 except KeyError:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'EST'
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'EST'
http://localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 6/15
10/2/2018 Untitled1
In [15]: df['EST']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3062 try:
-> 3063 return self._engine.get_loc(key)
3064 except KeyError:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'EST'
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'EST'
http://localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 7/15
10/2/2018 Untitled1
Out[16]:
Temperature DewPoint Humidity Sea Level PressureIn VisibilityMiles WindSpeedMPH PrecipitationIn CloudCover Events WindDirDegrees Unnamed: 11
EST
http://localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 8/15
10/2/2018 Untitled1
Out[17]:
Temperature DewPoint Humidity Sea Level PressureIn VisibilityMiles WindSpeedMPH PrecipitationIn CloudCover Events WindDirDegrees Unnamed: 11
EST
http://localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 9/15
10/2/2018 Untitled1
In [18]: dt = df['Temperature']
dt
Out[18]: EST
2016-01-01 38
2016-01-02 36
2016-01-03 40
2016-01-04 25
2016-01-05 20
2016-01-06 33
2016-01-07 39
2016-01-08 39
2016-01-09 44
2016-01-10 50
2016-01-11 33
2016-01-12 35
2016-01-13 26
2016-01-14 30
2016-01-15 43
2016-01-16 47
2016-01-17 36
2016-01-18 25
2016-01-19 22
2016-01-20 32
2016-01-21 31
2016-01-22 26
2016-01-23 26
2016-01-24 28
2016-01-25 34
2016-01-26 43
2016-01-27 41
2016-01-28 37
2016-01-29 36
2016-01-30 34
2016-01-31 46
Name: Temperature, dtype: int64
In [19]: dt=dt.sort_index(ascending=True)
http://localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 10/15
10/2/2018 Untitled1
In [20]: dt
Out[20]: EST
2016-01-01 38
2016-01-02 36
2016-01-03 40
2016-01-04 25
2016-01-05 20
2016-01-06 33
2016-01-07 39
2016-01-08 39
2016-01-09 44
2016-01-10 50
2016-01-11 33
2016-01-12 35
2016-01-13 26
2016-01-14 30
2016-01-15 43
2016-01-16 47
2016-01-17 36
2016-01-18 25
2016-01-19 22
2016-01-20 32
2016-01-21 31
2016-01-22 26
2016-01-23 26
2016-01-24 28
2016-01-25 34
2016-01-26 43
2016-01-27 41
2016-01-28 37
2016-01-29 36
2016-01-30 34
2016-01-31 46
Name: Temperature, dtype: int64
In [21]: dt=dt.sort_index(descending=True)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-9954d923f2ab> in <module>()
----> 1 dt=dt.sort_index(descending=True)
http://localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 11/15
10/2/2018 Untitled1
In [22]: dt
Out[22]: EST
2016-01-01 38
2016-01-02 36
2016-01-03 40
2016-01-04 25
2016-01-05 20
2016-01-06 33
2016-01-07 39
2016-01-08 39
2016-01-09 44
2016-01-10 50
2016-01-11 33
2016-01-12 35
2016-01-13 26
2016-01-14 30
2016-01-15 43
2016-01-16 47
2016-01-17 36
2016-01-18 25
2016-01-19 22
2016-01-20 32
2016-01-21 31
2016-01-22 26
2016-01-23 26
2016-01-24 28
2016-01-25 34
2016-01-26 43
2016-01-27 41
2016-01-28 37
2016-01-29 36
2016-01-30 34
2016-01-31 46
Name: Temperature, dtype: int64
In [23]: dt.plot(label='Temp')
In [24]: plt.legend()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-24-a140b03f3a4c> in <module>()
----> 1 plt.legend()
In [26]: plt.legend()
http://localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 12/15
10/2/2018 Untitled1
In [27]: dt.plot(label='Temp')
In [28]: plt.show()
In [29]: mavg=pd.roll
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-29-77fe202408bd> in <module>()
----> 1 mavg=pd.roll
In [30]: plt.plot(dt)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-31-390c8a7895fb> in <module>()
----> 1 data_log = np.log(dt)
http://localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 13/15
10/2/2018 Untitled1
In [35]: plt.plot(Ddata_log)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-37-3fff420e0e3a> in <module>()
----> 1 plot_acf(series,alpha =.05, lags=31)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-38-e5c07eb1dde7> in <module>()
----> 1 plot.acf(series,alpha =.05, lags=31)
http://localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 14/15
10/2/2018 Untitled1
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-44-3fff420e0e3a> in <module>()
----> 1 plot_acf(series,alpha =.05, lags=31)
~\Anaconda3\lib\site-packages\statsmodels\graphics\tsaplots.py in plot_acf(x, ax, lags, alpha, use_vlines, unbiased, fft, title, zero, vlines_kwargs, **kwargs)
132 else:
133 acf_x, confint = acf(x, nlags=nlags, alpha=alpha, fft=fft,
--> 134 unbiased=unbiased)
135
136 _plot_corr(ax, title, acf_x, confint, lags, irregular, use_vlines,
ValueError: operands could not be broadcast together with shapes (30,) (29,) (30,)
Out[45]:
http://localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 15/15