Microsoft Windows [版本 10.0.15063]
(c) 2017 Microsoft Corporation。保留所有权利。
(c) 2017 Microsoft Corporation。保留所有权利。
C:\Users\DELL>ipython
Python 3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
Python 3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 5.3.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import numpy as np
In [2]: data=np.array([1,3,4,8])
In [3]: data
Out[3]: array([1, 3, 4, 8])
Out[3]: array([1, 3, 4, 8])
In [4]: data.shape
Out[4]: (4,)
Out[4]: (4,)
In [5]: data.dtype
Out[5]: dtype('int32')
Out[5]: dtype('int32')
In [6]: data[1]
Out[6]: 3
Out[6]: 3
In [7]: data[1]=9
In [8]: data[1]
Out[8]: 9
Out[8]: 9
In [9]: data=np.array([[1,2,3],[4,5,6]]
...: )
...: )
In [10]: data
Out[10]:
array([[1, 2, 3],
[4, 5, 6]])
Out[10]:
array([[1, 2, 3],
[4, 5, 6]])
In [11]: data.shape
Out[11]: (2, 3)
Out[11]: (2, 3)
In [12]: data[1,2]
Out[12]: 6
Out[12]: 6
In [13]: np.arange(10)
Out[13]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Out[13]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [14]: range(10)
Out[14]: range(0, 10)
Out[14]: range(0, 10)
In [15]: data.reshape(2,5)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-15-7409f9ed58cd> in <module>()
----> 1 data.reshape(2,5)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-15-7409f9ed58cd> in <module>()
----> 1 data.reshape(2,5)
ValueError: cannot reshape array of size 6 into shape (2,5)
In [16]: np.zeros(1,2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-16-1f6dda62bd29> in <module>()
----> 1 np.zeros(1,2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-16-1f6dda62bd29> in <module>()
----> 1 np.zeros(1,2)
TypeError: data type not understood
In [17]: np.ones((2,3,3
...: ))
Out[17]:
array([[[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]],
...: ))
Out[17]:
array([[[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]],
[[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]]])
[ 1., 1., 1.],
[ 1., 1., 1.]]])
In [18]: np.eye(4)
Out[18]:
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
Out[18]:
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
In [19]: np.arange(16).reshape(4,4,)
Out[19]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
Out[19]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
In [20]: data=np.arange(100,step=10)
In [21]: data
Out[21]: array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
Out[21]: array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
In [22]: data[2:5
...:
...: ]
Out[22]: array([20, 30, 40])
...:
...: ]
Out[22]: array([20, 30, 40])
In [23]: data(:3)
File "<ipython-input-23-6a456dc0838a>", line 1
data(:3)
^
SyntaxError: invalid syntax
File "<ipython-input-23-6a456dc0838a>", line 1
data(:3)
^
SyntaxError: invalid syntax
In [24]: data[5:]=-1
In [25]: data
Out[25]: array([ 0, 10, 20, 30, 40, -1, -1, -1, -1, -1])
Out[25]: array([ 0, 10, 20, 30, 40, -1, -1, -1, -1, -1])
In [26]: data=np.arange(16).reshape(4,4)
In [27]: data
Out[27]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
Out[27]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
In [28]: data[:,2;4]
File "<ipython-input-28-d3b37d080c4a>", line 1
data[:,2;4]
^
SyntaxError: invalid syntax
File "<ipython-input-28-d3b37d080c4a>", line 1
data[:,2;4]
^
SyntaxError: invalid syntax
In [29]: data[1:3]
Out[29]:
array([[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [30]: data[:,2:4]
Out[30]:
array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])
Out[30]:
array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])
In [31]: data[1:3,2:4]
Out[31]:
array([[ 6, 7],
[10, 11]])
Out[31]:
array([[ 6, 7],
[10, 11]])
In [32]: data
Out[32]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
Out[32]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
In [33]: data[[1,3],[2,3]]
Out[33]: array([ 6, 15])
Out[33]: array([ 6, 15])
In [34]: data
Out[34]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
Out[34]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
In [35]: data>10
Out[35]:
array([[False, False, False, False],
[False, False, False, False],
[False, False, False, True],
[ True, True, True, True]], dtype=bool)
Out[35]:
array([[False, False, False, False],
[False, False, False, False],
[False, False, False, True],
[ True, True, True, True]], dtype=bool)
In [36]: import pandas as pd
In [37]: s=pd.Series([1,3,5,np.NaN,8,4])
In [38]: s
Out[38]:
0 1.0
1 3.0
2 5.0
3 NaN
4 8.0
5 4.0
dtype: float64
Out[38]:
0 1.0
1 3.0
2 5.0
3 NaN
4 8.0
5 4.0
dtype: float64
In [39]: dates=pd.date_range('20160808',periods=6)
In [40]: dates
Out[40]:
DatetimeIndex(['2016-08-08', '2016-08-09', '2016-08-10', '2016-08-11',
'2016-08-12', '2016-08-13'],
dtype='datetime64[ns]', freq='D')
Out[40]:
DatetimeIndex(['2016-08-08', '2016-08-09', '2016-08-10', '2016-08-11',
'2016-08-12', '2016-08-13'],
dtype='datetime64[ns]', freq='D')
In [41]: date=pd.DateFrame(np.random.randn(6,4),index=dates,columns=list('ABCD')
...: )
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-41-2aaa440e68ce> in <module>()
----> 1 date=pd.DateFrame(np.random.randn(6,4),index=dates,columns=list('ABCD')
2 )
...: )
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-41-2aaa440e68ce> in <module>()
----> 1 date=pd.DateFrame(np.random.randn(6,4),index=dates,columns=list('ABCD')
2 )
AttributeError: module 'pandas' has no attribute 'DateFrame'
In [42]: date=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD')
...: )
...: )
In [43]: data=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD')
...: )
...: )
In [44]: data
Out[44]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
Out[44]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
In [45]: data.shape
Out[45]: (6, 4)
Out[45]: (6, 4)
In [46]: data.values
Out[46]:
array([[-1.23828511, 1.19954171, -1.01551497, -0.57037752],
[ 0.0561472 , 0.56120799, 0.11490719, 2.23290696],
[-1.35681292, 0.34696272, -1.61481278, -1.12554813],
[ 1.68735839, -2.60721149, 0.86463635, 0.89197538],
[ 0.74881434, -0.40185512, 0.41037125, -0.40958799],
[ 0.15844321, -1.98083492, -2.38405078, -0.68294166]])
Out[46]:
array([[-1.23828511, 1.19954171, -1.01551497, -0.57037752],
[ 0.0561472 , 0.56120799, 0.11490719, 2.23290696],
[-1.35681292, 0.34696272, -1.61481278, -1.12554813],
[ 1.68735839, -2.60721149, 0.86463635, 0.89197538],
[ 0.74881434, -0.40185512, 0.41037125, -0.40958799],
[ 0.15844321, -1.98083492, -2.38405078, -0.68294166]])
In [47]: d={'A':1,'B':pd.Timestamp('20171028'),'C':range(4),'D':np.arange
...: (4)}
...: (4)}
In [48]: d
Out[48]:
{'A': 1,
'B': Timestamp('2017-10-28 00:00:00'),
'C': range(0, 4),
'D': array([0, 1, 2, 3])}
Out[48]:
{'A': 1,
'B': Timestamp('2017-10-28 00:00:00'),
'C': range(0, 4),
'D': array([0, 1, 2, 3])}
In [49]: df=pd.DataFrame(d)
In [50]: df
Out[50]:
A B C D
0 1 2017-10-28 0 0
1 1 2017-10-28 1 1
2 1 2017-10-28 2 2
3 1 2017-10-28 3 3
Out[50]:
A B C D
0 1 2017-10-28 0 0
1 1 2017-10-28 1 1
2 1 2017-10-28 2 2
3 1 2017-10-28 3 3
In [51]: df=pd.DataFrame(d,index=list('abcd')
...: )
...: )
In [52]: df
Out[52]:
A B C D
a 1 2017-10-28 0 0
b 1 2017-10-28 1 1
c 1 2017-10-28 2 2
d 1 2017-10-28 3 3
Out[52]:
A B C D
a 1 2017-10-28 0 0
b 1 2017-10-28 1 1
c 1 2017-10-28 2 2
d 1 2017-10-28 3 3
In [53]: df.B
Out[53]:
a 2017-10-28
b 2017-10-28
c 2017-10-28
d 2017-10-28
Name: B, dtype: datetime64[ns]
Out[53]:
a 2017-10-28
b 2017-10-28
c 2017-10-28
d 2017-10-28
Name: B, dtype: datetime64[ns]
In [54]: data
Out[54]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
Out[54]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
In [55]: data.head()
Out[55]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
Out[55]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
In [56]: data.tail()
Out[56]:
A B C D
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
Out[56]:
A B C D
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
In [57]: data.tail(3)
Out[57]:
A B C D
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
Out[57]:
A B C D
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
In [58]: data.index
Out[58]:
DatetimeIndex(['2016-08-08', '2016-08-09', '2016-08-10', '2016-08-11',
'2016-08-12', '2016-08-13'],
dtype='datetime64[ns]', freq='D')
Out[58]:
DatetimeIndex(['2016-08-08', '2016-08-09', '2016-08-10', '2016-08-11',
'2016-08-12', '2016-08-13'],
dtype='datetime64[ns]', freq='D')
In [59]: data.column
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-59-1d4bd3e086c3> in <module>()
----> 1 data.column
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-59-1d4bd3e086c3> in <module>()
----> 1 data.column
E:\python\anaconda\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
2968 if name in self._info_axis:
2969 return self[name]
-> 2970 return object.__getattribute__(self, name)
2971
2972 def __setattr__(self, name, value):
2968 if name in self._info_axis:
2969 return self[name]
-> 2970 return object.__getattribute__(self, name)
2971
2972 def __setattr__(self, name, value):
AttributeError: 'DataFrame' object has no attribute 'column'
In [60]: data.columns
Out[60]: Index(['A', 'B', 'C', 'D'], dtype='object')
Out[60]: Index(['A', 'B', 'C', 'D'], dtype='object')
In [61]: data.values
Out[61]:
array([[-1.23828511, 1.19954171, -1.01551497, -0.57037752],
[ 0.0561472 , 0.56120799, 0.11490719, 2.23290696],
[-1.35681292, 0.34696272, -1.61481278, -1.12554813],
[ 1.68735839, -2.60721149, 0.86463635, 0.89197538],
[ 0.74881434, -0.40185512, 0.41037125, -0.40958799],
[ 0.15844321, -1.98083492, -2.38405078, -0.68294166]])
Out[61]:
array([[-1.23828511, 1.19954171, -1.01551497, -0.57037752],
[ 0.0561472 , 0.56120799, 0.11490719, 2.23290696],
[-1.35681292, 0.34696272, -1.61481278, -1.12554813],
[ 1.68735839, -2.60721149, 0.86463635, 0.89197538],
[ 0.74881434, -0.40185512, 0.41037125, -0.40958799],
[ 0.15844321, -1.98083492, -2.38405078, -0.68294166]])
In [62]: data.describe()
Out[62]:
A B C D
count 6.000000 6.000000 6.000000 6.000000
mean 0.009278 -0.480365 -0.604077 0.056071
std 1.167042 1.508143 1.269830 1.264040
min -1.356813 -2.607211 -2.384051 -1.125548
25% -0.914677 -1.586090 -1.464988 -0.654801
50% 0.107295 -0.027446 -0.450304 -0.489983
75% 0.601222 0.507647 0.336505 0.566585
max 1.687358 1.199542 0.864636 2.232907
Out[62]:
A B C D
count 6.000000 6.000000 6.000000 6.000000
mean 0.009278 -0.480365 -0.604077 0.056071
std 1.167042 1.508143 1.269830 1.264040
min -1.356813 -2.607211 -2.384051 -1.125548
25% -0.914677 -1.586090 -1.464988 -0.654801
50% 0.107295 -0.027446 -0.450304 -0.489983
75% 0.601222 0.507647 0.336505 0.566585
max 1.687358 1.199542 0.864636 2.232907
In [63]: data.T
Out[63]:
2016-08-08 2016-08-09 2016-08-10 2016-08-11 2016-08-12 2016-08-13
A -1.238285 0.056147 -1.356813 1.687358 0.748814 0.158443
B 1.199542 0.561208 0.346963 -2.607211 -0.401855 -1.980835
C -1.015515 0.114907 -1.614813 0.864636 0.410371 -2.384051
D -0.570378 2.232907 -1.125548 0.891975 -0.409588 -0.682942
Out[63]:
2016-08-08 2016-08-09 2016-08-10 2016-08-11 2016-08-12 2016-08-13
A -1.238285 0.056147 -1.356813 1.687358 0.748814 0.158443
B 1.199542 0.561208 0.346963 -2.607211 -0.401855 -1.980835
C -1.015515 0.114907 -1.614813 0.864636 0.410371 -2.384051
D -0.570378 2.232907 -1.125548 0.891975 -0.409588 -0.682942
In [64]: data.index
Out[64]:
DatetimeIndex(['2016-08-08', '2016-08-09', '2016-08-10', '2016-08-11',
'2016-08-12', '2016-08-13'],
dtype='datetime64[ns]', freq='D')
Out[64]:
DatetimeIndex(['2016-08-08', '2016-08-09', '2016-08-10', '2016-08-11',
'2016-08-12', '2016-08-13'],
dtype='datetime64[ns]', freq='D')
In [65]: data
Out[65]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
Out[65]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
In [66]: data.sort_index(axis=1)
Out[66]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
Out[66]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
In [67]: data.sort_index(axis=1,ascending=False)
Out[67]:
D C B A
2016-08-08 -0.570378 -1.015515 1.199542 -1.238285
2016-08-09 2.232907 0.114907 0.561208 0.056147
2016-08-10 -1.125548 -1.614813 0.346963 -1.356813
2016-08-11 0.891975 0.864636 -2.607211 1.687358
2016-08-12 -0.409588 0.410371 -0.401855 0.748814
2016-08-13 -0.682942 -2.384051 -1.980835 0.158443
Out[67]:
D C B A
2016-08-08 -0.570378 -1.015515 1.199542 -1.238285
2016-08-09 2.232907 0.114907 0.561208 0.056147
2016-08-10 -1.125548 -1.614813 0.346963 -1.356813
2016-08-11 0.891975 0.864636 -2.607211 1.687358
2016-08-12 -0.409588 0.410371 -0.401855 0.748814
2016-08-13 -0.682942 -2.384051 -1.980835 0.158443
In [68]: data.sort_index(axis=0)
Out[68]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
Out[68]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
In [69]: data
Out[69]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
Out[69]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
In [70]: data.sort_index(axis=0,ascending=False)
Out[70]:
A B C D
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
Out[70]:
A B C D
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
In [71]: data.sort_values(by='A')
Out[71]:
A B C D
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-11 1.687358 -2.607211 0.864636 0.891975
Out[71]:
A B C D
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-11 1.687358 -2.607211 0.864636 0.891975
In [72]: data.A
Out[72]:
2016-08-08 -1.238285
2016-08-09 0.056147
2016-08-10 -1.356813
2016-08-11 1.687358
2016-08-12 0.748814
2016-08-13 0.158443
Freq: D, Name: A, dtype: float64
Out[72]:
2016-08-08 -1.238285
2016-08-09 0.056147
2016-08-10 -1.356813
2016-08-11 1.687358
2016-08-12 0.748814
2016-08-13 0.158443
Freq: D, Name: A, dtype: float64
In [73]: data[2:4]
Out[73]:
A B C D
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
Out[73]:
A B C D
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
In [74]: data['20160808':'20160811']
Out[74]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
Out[74]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
In [75]: data['A':'B']
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-75-7b6a8e64bd7d> in <module>()
----> 1 data['A':'B']
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-75-7b6a8e64bd7d> in <module>()
----> 1 data['A':'B']
E:\python\anaconda\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2048
2049 # see if we can slice the rows
-> 2050 indexer = convert_to_index_sliceable(self, key)
2051 if indexer is not None:
2052 return self._getitem_slice(indexer)
2048
2049 # see if we can slice the rows
-> 2050 indexer = convert_to_index_sliceable(self, key)
2051 if indexer is not None:
2052 return self._getitem_slice(indexer)
E:\python\anaconda\lib\site-packages\pandas\core\indexing.py in convert_to_index_sliceable(obj, key)
1894 idx = obj.index
1895 if isinstance(key, slice):
-> 1896 return idx._convert_slice_indexer(key, kind='getitem')
1897
1898 elif isinstance(key, compat.string_types):
1894 idx = obj.index
1895 if isinstance(key, slice):
-> 1896 return idx._convert_slice_indexer(key, kind='getitem')
1897
1898 elif isinstance(key, compat.string_types):
E:\python\anaconda\lib\site-packages\pandas\core\indexes\base.py in _convert_slice_indexer(self, key, kind)
1355 else:
1356 try:
-> 1357 indexer = self.slice_indexer(start, stop, step, kind=kind)
1358 except Exception:
1359 if is_index_slice:
1355 else:
1356 try:
-> 1357 indexer = self.slice_indexer(start, stop, step, kind=kind)
1358 except Exception:
1359 if is_index_slice:
E:\python\anaconda\lib\site-packages\pandas\core\indexes\datetimes.py in slice_indexer(self, start, end, step, kind)
1513
1514 try:
-> 1515 return Index.slice_indexer(self, start, end, step, kind=kind)
1516 except KeyError:
1517 # For historical reasons DatetimeIndex by default supports
1513
1514 try:
-> 1515 return Index.slice_indexer(self, start, end, step, kind=kind)
1516 except KeyError:
1517 # For historical reasons DatetimeIndex by default supports
E:\python\anaconda\lib\site-packages\pandas\core\indexes\base.py in slice_indexer(self, start, end, step, kind)
3299 """
3300 start_slice, end_slice = self.slice_locs(start, end, step=step,
-> 3301 kind=kind)
3302
3303 # return a slice
3299 """
3300 start_slice, end_slice = self.slice_locs(start, end, step=step,
-> 3301 kind=kind)
3302
3303 # return a slice
E:\python\anaconda\lib\site-packages\pandas\core\indexes\base.py in slice_locs(self, start, end, step, kind)
3487 start_slice = None
3488 if start is not None:
-> 3489 start_slice = self.get_slice_bound(start, 'left', kind)
3490 if start_slice is None:
3491 start_slice = 0
3487 start_slice = None
3488 if start is not None:
-> 3489 start_slice = self.get_slice_bound(start, 'left', kind)
3490 if start_slice is None:
3491 start_slice = 0
E:\python\anaconda\lib\site-packages\pandas\core\indexes\base.py in get_slice_bound(self, label, side, kind)
3426 # For datetime indices label may be a string that has to be converted
3427 # to datetime boundary according to its resolution.
-> 3428 label = self._maybe_cast_slice_bound(label, side, kind)
3429
3430 # we need to look up the label
3426 # For datetime indices label may be a string that has to be converted
3427 # to datetime boundary according to its resolution.
-> 3428 label = self._maybe_cast_slice_bound(label, side, kind)
3429
3430 # we need to look up the label
E:\python\anaconda\lib\site-packages\pandas\core\indexes\datetimes.py in _maybe_cast_slice_bound(self, label, side, kind)
1465 freq = getattr(self, 'freqstr',
1466 getattr(self, 'inferred_freq', None))
-> 1467 _, parsed, reso = parse_time_string(label, freq)
1468 lower, upper = self._parsed_string_to_bounds(reso, parsed)
1469 # lower, upper form the half-open interval:
1465 freq = getattr(self, 'freqstr',
1466 getattr(self, 'inferred_freq', None))
-> 1467 _, parsed, reso = parse_time_string(label, freq)
1468 lower, upper = self._parsed_string_to_bounds(reso, parsed)
1469 # lower, upper form the half-open interval:
E:\python\anaconda\lib\site-packages\pandas\core\tools\datetimes.py in parse_time_string(arg, freq, dayfirst, yearfirst)
732 return tslib.parse_datetime_string_with_reso(arg, freq=freq,
733 dayfirst=dayfirst,
--> 734 yearfirst=yearfirst)
735
736
732 return tslib.parse_datetime_string_with_reso(arg, freq=freq,
733 dayfirst=dayfirst,
--> 734 yearfirst=yearfirst)
735
736
pandas\_libs\tslib.pyx in pandas._libs.tslib.parse_datetime_string_with_reso (pandas\_libs\tslib.c:35618)()
ValueError: Given date string not likely a datetime.
In [76]: data.loc['20160808':'20160811']
Out[76]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
Out[76]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
In [77]: data.iloc[2:4]
Out[77]:
A B C D
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
Out[77]:
A B C D
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
In [78]: data.loc[:,['B':'C']]
File "<ipython-input-78-b119f70bebca>", line 1
data.loc[:,['B':'C']]
^
SyntaxError: invalid syntax
File "<ipython-input-78-b119f70bebca>", line 1
data.loc[:,['B':'C']]
^
SyntaxError: invalid syntax
In [79]: data.loc[:,['B':'C']]
File "<ipython-input-79-b119f70bebca>", line 1
data.loc[:,['B':'C']]
^
SyntaxError: invalid syntax
In [80]: data.loc[:,['B','C']]
Out[80]:
B C
2016-08-08 1.199542 -1.015515
2016-08-09 0.561208 0.114907
2016-08-10 0.346963 -1.614813
2016-08-11 -2.607211 0.864636
2016-08-12 -0.401855 0.410371
2016-08-13 -1.980835 -2.384051
In [81]: data.loc['20160810':,['B','C']]
Out[81]:
B C
2016-08-10 0.346963 -1.614813
2016-08-11 -2.607211 0.864636
2016-08-12 -0.401855 0.410371
2016-08-13 -1.980835 -2.384051
Out[81]:
B C
2016-08-10 0.346963 -1.614813
2016-08-11 -2.607211 0.864636
2016-08-12 -0.401855 0.410371
2016-08-13 -1.980835 -2.384051
In [82]: data.loc['20160808','B']
Out[82]: 1.1995417134957607
Out[82]: 1.1995417134957607
In [83]: data.at[pd.Timestamp('20160808'),'B']
Out[83]: 1.1995417134957607
Out[83]: 1.1995417134957607
In [84]: data.iloc[2:4,2:4]
Out[84]:
C D
2016-08-10 -1.614813 -1.125548
2016-08-11 0.864636 0.891975
Out[84]:
C D
2016-08-10 -1.614813 -1.125548
2016-08-11 0.864636 0.891975
In [85]: data.iloc[1,1]
Out[85]: 0.56120799012007772
Out[85]: 0.56120799012007772
In [86]: data.iat[1,1]
Out[86]: 0.56120799012007772
Out[86]: 0.56120799012007772
In [87]: data[data.A>0]
Out[87]:
A B C D
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
Out[87]:
A B C D
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
In [88]: data[data>0]
Out[88]:
A B C D
2016-08-08 NaN 1.199542 NaN NaN
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 NaN 0.346963 NaN NaN
2016-08-11 1.687358 NaN 0.864636 0.891975
2016-08-12 0.748814 NaN 0.410371 NaN
2016-08-13 0.158443 NaN NaN NaN
Out[88]:
A B C D
2016-08-08 NaN 1.199542 NaN NaN
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 NaN 0.346963 NaN NaN
2016-08-11 1.687358 NaN 0.864636 0.891975
2016-08-12 0.748814 NaN 0.410371 NaN
2016-08-13 0.158443 NaN NaN NaN
In [89]: data2=data.copy()
In [90]: data2
Out[90]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
Out[90]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
In [91]: tag=['a']*2+['b']*2+['c']*2
In [92]: data2['TAG']=tag
In [93]: data2
Out[93]:
A B C D TAG
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378 a
2016-08-09 0.056147 0.561208 0.114907 2.232907 a
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548 b
2016-08-11 1.687358 -2.607211 0.864636 0.891975 b
2016-08-12 0.748814 -0.401855 0.410371 -0.409588 c
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942 c
Out[93]:
A B C D TAG
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378 a
2016-08-09 0.056147 0.561208 0.114907 2.232907 a
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548 b
2016-08-11 1.687358 -2.607211 0.864636 0.891975 b
2016-08-12 0.748814 -0.401855 0.410371 -0.409588 c
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942 c
In [94]: data
Out[94]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
Out[94]:
A B C D
2016-08-08 -1.238285 1.199542 -1.015515 -0.570378
2016-08-09 0.056147 0.561208 0.114907 2.232907
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548
2016-08-11 1.687358 -2.607211 0.864636 0.891975
2016-08-12 0.748814 -0.401855 0.410371 -0.409588
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942
In [95]: data2.iat[0,0]=1
In [96]: data2
Out[96]:
A B C D TAG
2016-08-08 1.000000 1.199542 -1.015515 -0.570378 a
2016-08-09 0.056147 0.561208 0.114907 2.232907 a
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548 b
2016-08-11 1.687358 -2.607211 0.864636 0.891975 b
2016-08-12 0.748814 -0.401855 0.410371 -0.409588 c
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942 c
Out[96]:
A B C D TAG
2016-08-08 1.000000 1.199542 -1.015515 -0.570378 a
2016-08-09 0.056147 0.561208 0.114907 2.232907 a
2016-08-10 -1.356813 0.346963 -1.614813 -1.125548 b
2016-08-11 1.687358 -2.607211 0.864636 0.891975 b
2016-08-12 0.748814 -0.401855 0.410371 -0.409588 c
2016-08-13 0.158443 -1.980835 -2.384051 -0.682942 c
In [97]: data2.A=range(6)
In [98]: data2
Out[98]:
A B C D TAG
2016-08-08 0 1.199542 -1.015515 -0.570378 a
2016-08-09 1 0.561208 0.114907 2.232907 a
2016-08-10 2 0.346963 -1.614813 -1.125548 b
2016-08-11 3 -2.607211 0.864636 0.891975 b
2016-08-12 4 -0.401855 0.410371 -0.409588 c
2016-08-13 5 -1.980835 -2.384051 -0.682942 c
Out[98]:
A B C D TAG
2016-08-08 0 1.199542 -1.015515 -0.570378 a
2016-08-09 1 0.561208 0.114907 2.232907 a
2016-08-10 2 0.346963 -1.614813 -1.125548 b
2016-08-11 3 -2.607211 0.864636 0.891975 b
2016-08-12 4 -0.401855 0.410371 -0.409588 c
2016-08-13 5 -1.980835 -2.384051 -0.682942 c
In [99]: data.B=200
In [100]: data
Out[100]:
A B C D
2016-08-08 -1.238285 200 -1.015515 -0.570378
2016-08-09 0.056147 200 0.114907 2.232907
2016-08-10 -1.356813 200 -1.614813 -1.125548
2016-08-11 1.687358 200 0.864636 0.891975
2016-08-12 0.748814 200 0.410371 -0.409588
2016-08-13 0.158443 200 -2.384051 -0.682942
Out[100]:
A B C D
2016-08-08 -1.238285 200 -1.015515 -0.570378
2016-08-09 0.056147 200 0.114907 2.232907
2016-08-10 -1.356813 200 -1.614813 -1.125548
2016-08-11 1.687358 200 0.864636 0.891975
2016-08-12 0.748814 200 0.410371 -0.409588
2016-08-13 0.158443 200 -2.384051 -0.682942
In [101]: data.at[Timestamp('20160808'),'A']=5
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-101-6168c754ecfd> in <module>()
----> 1 data.at[Timestamp('20160808'),'A']=5
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-101-6168c754ecfd> in <module>()
----> 1 data.at[Timestamp('20160808'),'A']=5
NameError: name 'Timestamp' is not defined
In [102]: data.at[pd.
...: Timestamp('20160808'),'A']=5
...: Timestamp('20160808'),'A']=5
In [103]: data
Out[103]:
A B C D
2016-08-08 5.000000 200 -1.015515 -0.570378
2016-08-09 0.056147 200 0.114907 2.232907
2016-08-10 -1.356813 200 -1.614813 -1.125548
2016-08-11 1.687358 200 0.864636 0.891975
2016-08-12 0.748814 200 0.410371 -0.409588
2016-08-13 0.158443 200 -2.384051 -0.682942
Out[103]:
A B C D
2016-08-08 5.000000 200 -1.015515 -0.570378
2016-08-09 0.056147 200 0.114907 2.232907
2016-08-10 -1.356813 200 -1.614813 -1.125548
2016-08-11 1.687358 200 0.864636 0.891975
2016-08-12 0.748814 200 0.410371 -0.409588
2016-08-13 0.158443 200 -2.384051 -0.682942
In [104]: data.iloc[:,2:5]=1000
In [105]: data
Out[105]:
A B C D
2016-08-08 5.000000 200 1000 1000
2016-08-09 0.056147 200 1000 1000
2016-08-10 -1.356813 200 1000 1000
2016-08-11 1.687358 200 1000 1000
2016-08-12 0.748814 200 1000 1000
2016-08-13 0.158443 200 1000 1000
Out[105]:
A B C D
2016-08-08 5.000000 200 1000 1000
2016-08-09 0.056147 200 1000 1000
2016-08-10 -1.356813 200 1000 1000
2016-08-11 1.687358 200 1000 1000
2016-08-12 0.748814 200 1000 1000
2016-08-13 0.158443 200 1000 1000
In [106]: