10 Minutes to pandas

pandas 入门(一)-新乡seo|网站优化,网站建设_微信公众号:zeropython—昊天博客

请参阅官方文档

In [1]:
In [2]:

创建数据集对象

In [3]:
Out[3]:
In [4]:
Out[4]:
In [5]:
Out[5]:
A B C D
2016-03-01 1.188983 -1.150119 -0.700588 0.439065
2016-03-02 -2.041544 1.084507 -0.335441 1.969754
2016-03-03 1.204151 -1.277714 -0.230671 0.629063
2016-03-04 -0.352351 -1.701585 -0.034294 -0.330139
2016-03-05 0.627601 -0.292939 0.457975 2.262402
2016-03-06 -1.121869 -0.533223 0.627452 0.412665
In [6]:
Out[6]:
In [7]:
Out[7]:
A B C D E F
0 1 2016-03-01 0 5 text AA
1 1 2016-03-01 1 6 text BB
2 1 2016-03-01 2 7 text CC
3 1 2016-03-01 3 8 text DD
In [8]:
Out[8]:
In [9]:
Out[9]:
In [10]:
Out[10]:

查看数据

In [11]:
Out[11]:
A B C D
2016-03-01 1.313419 0.826457 -1.574146 0.525008
2016-03-02 0.028397 -1.009349 0.327014 0.918248
2016-03-03 -0.857008 -1.682695 0.646229 -0.183377
2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619
2016-03-05 -0.448713 -0.163651 -1.230295 1.106656
2016-03-06 -0.267867 0.092313 -0.480238 -0.809923
In [12]:
Out[12]:
In [13]:
Out[13]:
A B C D
2016-03-01 1.313419 0.826457 -1.574146 0.525008
2016-03-02 0.028397 -1.009349 0.327014 0.918248
2016-03-03 -0.857008 -1.682695 0.646229 -0.183377
2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619
2016-03-05 -0.448713 -0.163651 -1.230295 1.106656
In [14]:
Out[14]:
A B C D
2016-03-01 1.313419 0.826457 -1.574146 0.525008
2016-03-02 0.028397 -1.009349 0.327014 0.918248
2016-03-03 -0.857008 -1.682695 0.646229 -0.183377
In [15]:
Out[15]:
A B C D
2016-03-02 0.028397 -1.009349 0.327014 0.918248
2016-03-03 -0.857008 -1.682695 0.646229 -0.183377
2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619
2016-03-05 -0.448713 -0.163651 -1.230295 1.106656
2016-03-06 -0.267867 0.092313 -0.480238 -0.809923
In [16]:
Out[16]:
A B C D
2016-03-05 -0.448713 -0.163651 -1.230295 1.106656
2016-03-06 -0.267867 0.092313 -0.480238 -0.809923
In [17]:
Out[17]:
In [18]:
Out[18]:
In [19]:
Out[19]:
In [20]:
Out[20]:
A B C D
count 6.000000 6.000000 6.000000 6.000000
mean -0.224110 -0.571431 -0.571043 0.240165
std 0.856808 0.983304 0.898112 0.734900
min -1.112885 -1.682695 -1.574146 -0.809923
25% -0.754935 -1.371084 -1.201427 -0.166438
50% -0.358290 -0.586500 -0.797531 0.204694
75% -0.045669 0.028322 0.125201 0.819938
max 1.313419 0.826457 0.646229 1.106656
In [21]:
Out[21]:
2016-03-01 00:00:00 2016-03-02 00:00:00 2016-03-03 00:00:00 2016-03-04 00:00:00 2016-03-05 00:00:00 2016-03-06 00:00:00
A 1.313419 0.028397 -0.857008 -1.112885 -0.448713 -0.267867
B 0.826457 -1.009349 -1.682695 -1.491662 -0.163651 0.092313
C -1.574146 0.327014 0.646229 -1.114824 -1.230295 -0.480238
D 0.525008 0.918248 -0.183377 -0.115619 1.106656 -0.809923
In [22]:
Out[22]:
In [23]:
Out[23]:
D C B A
2016-03-01 0.525008 -1.574146 0.826457 1.313419
2016-03-02 0.918248 0.327014 -1.009349 0.028397
2016-03-03 -0.183377 0.646229 -1.682695 -0.857008
2016-03-04 -0.115619 -1.114824 -1.491662 -1.112885
2016-03-05 1.106656 -1.230295 -0.163651 -0.448713
2016-03-06 -0.809923 -0.480238 0.092313 -0.267867
In [24]:
Out[24]:
A B C D
2016-03-01 1.313419 0.826457 -1.574146 0.525008
2016-03-05 -0.448713 -0.163651 -1.230295 1.106656
2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619
2016-03-06 -0.267867 0.092313 -0.480238 -0.809923
2016-03-02 0.028397 -1.009349 0.327014 0.918248
2016-03-03 -0.857008 -1.682695 0.646229 -0.183377

数据选择

In [25]:
Out[25]:
In [26]:
Out[26]:
A B C D
2016-03-03 -0.857008 -1.682695 0.646229 -0.183377
2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619
In [27]:
Out[27]:
A B C D
2016-03-02 0.028397 -1.009349 0.327014 0.918248
2016-03-03 -0.857008 -1.682695 0.646229 -0.183377
2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619
2016-03-05 -0.448713 -0.163651 -1.230295 1.106656

通过标签选择

In [28]:
Out[28]:
In [29]:
Out[29]:
In [30]:
Out[30]:
A B
2016-03-01 1.313419 0.826457
2016-03-02 0.028397 -1.009349
2016-03-03 -0.857008 -1.682695
2016-03-04 -1.112885 -1.491662
2016-03-05 -0.448713 -0.163651
2016-03-06 -0.267867 0.092313
In [31]:
Out[31]:
A B
2016-03-01 1.313419 0.826457
2016-03-02 0.028397 -1.009349
2016-03-03 -0.857008 -1.682695
2016-03-04 -1.112885 -1.491662
2016-03-05 -0.448713 -0.163651
In [32]:
Out[32]:
In [33]:
Out[33]:

通过位置选择

In [34]:
Out[34]:
In [35]:
Out[35]:
A B
2016-03-03 -0.857008 -1.682695
2016-03-04 -1.112885 -1.491662
2016-03-05 -0.448713 -0.163651
In [36]:
Out[36]:
A B C D
2016-03-02 0.028397 -1.009349 0.327014 0.918248
2016-03-03 -0.857008 -1.682695 0.646229 -0.183377
2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619
2016-03-05 -0.448713 -0.163651 -1.230295 1.106656
In [37]:
Out[37]:
In [38]:
Out[38]:

布尔索引

In [39]:
Out[39]:
A B C D
2016-03-03 -0.857008 -1.682695 0.646229 -0.183377
2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619
2016-03-05 -0.448713 -0.163651 -1.230295 1.106656
2016-03-06 -0.267867 0.092313 -0.480238 -0.809923
In [40]:
Out[40]:
A B C D
2016-03-01 1.313419 0.826457 NaN 0.525008
2016-03-02 0.028397 NaN 0.327014 0.918248
2016-03-03 NaN NaN 0.646229 NaN
2016-03-04 NaN NaN NaN NaN
2016-03-05 NaN NaN NaN 1.106656
2016-03-06 NaN 0.092313 NaN NaN
In [41]:
In [42]:
Out[42]:
A B C D tag
2016-03-01 1.313419 0.826457 -1.574146 0.525008 a
2016-03-02 0.028397 -1.009349 0.327014 0.918248 a
2016-03-03 -0.857008 -1.682695 0.646229 -0.183377 b
2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619 b
2016-03-05 -0.448713 -0.163651 -1.230295 1.106656 c
2016-03-06 -0.267867 0.092313 -0.480238 -0.809923 c
In [43]:
Out[43]:
A B C D tag
2016-03-01 1.313419 0.826457 -1.574146 0.525008 a
2016-03-02 0.028397 -1.009349 0.327014 0.918248 a
2016-03-05 -0.448713 -0.163651 -1.230295 1.106656 c
2016-03-06 -0.267867 0.092313 -0.480238 -0.809923 c

修改数据

In [44]:
Out[44]:
A B C D tag
2016-03-01 1.313419 0.826457 -1.574146 0.525008 a
2016-03-02 0.028397 -1.009349 0.327014 0.918248 a
2016-03-03 -0.857008 -1.682695 0.646229 -0.183377 b
2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619 b
2016-03-05 -0.448713 -0.163651 -1.230295 1.106656 c
2016-03-06 -0.267867 0.092313 -0.480238 -0.809923 c
In [45]:
Out[45]:
In [46]:
In [47]:
Out[47]:
A B C D tag E
2016-03-01 1.313419 0.826457 -1.574146 0.525008 a 0
2016-03-02 0.028397 -1.009349 0.327014 0.918248 a 1
2016-03-03 -0.857008 -1.682695 0.646229 -0.183377 b 2
2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619 b 3
2016-03-05 -0.448713 -0.163651 -1.230295 1.106656 c 4
2016-03-06 -0.267867 0.092313 -0.480238 -0.809923 c 5
In [48]:
In [49]:
Out[49]:
A B C D tag E
2016-03-01 0.200000 0.826457 -1.574146 0.525008 a 0
2016-03-02 0.028397 -1.009349 0.327014 0.918248 a 1
2016-03-03 -0.857008 -1.682695 0.646229 -0.183377 b 2
2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619 b 3
2016-03-05 -0.448713 -0.163651 -1.230295 1.106656 c 4
2016-03-06 -0.267867 0.092313 -0.480238 -0.809923 c 5
In [50]:
In [51]:
Out[51]:
A B C D tag E
2016-03-01 0.400000 0.826457 -1.574146 0.525008 a 0
2016-03-02 0.028397 -1.009349 0.327014 0.918248 a 1
2016-03-03 -0.857008 -1.682695 0.646229 -0.183377 b 2
2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619 b 3
2016-03-05 -0.448713 -0.163651 -1.230295 1.106656 c 4
2016-03-06 -0.267867 0.092313 -0.480238 -0.809923 c 5
In [52]:
Out[52]:
A B C D tag E
2016-03-01 0.600000 0.826457 -1.574146 0.525008 a 0
2016-03-02 0.028397 -1.009349 0.327014 0.918248 a 1
2016-03-03 -0.857008 -1.682695 0.646229 -0.183377 b 2
2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619 b 3
2016-03-05 -0.448713 -0.163651 -1.230295 1.106656 c 4
2016-03-06 -0.267867 0.092313 -0.480238 -0.809923 c 5
In [53]:
Out[53]:
A B C D tag E
2016-03-01 10 0.826457 -1.574146 0.525008 a 0
2016-03-02 11 -1.009349 0.327014 0.918248 a 1
2016-03-03 12 -1.682695 0.646229 -0.183377 b 2
2016-03-04 13 -1.491662 -1.114824 -0.115619 b 3
2016-03-05 14 -0.163651 -1.230295 1.106656 c 4
2016-03-06 15 0.092313 -0.480238 -0.809923 c 5
In [54]:
Out[54]:
B C
2016-03-01 -0.826457 -1.574146
2016-03-02 -1.009349 -0.327014
2016-03-03 -1.682695 -0.646229
2016-03-04 -1.491662 -1.114824
2016-03-05 -0.163651 -1.230295
2016-03-06 -0.092313 -0.480238