<div id="js_content">
<p style="text-align: center"><img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-344c853535d9ebf8f135281eae72e294"></p>
<p>作者:youerning<br></p>
<p>来源:51CTO博客/Python数据科学</p>
<p style="text-align: left"><strong>一、数据对象</strong></p>
<p>pandas主要有两种数据对象:<strong>Series、DataFrame</strong><strong></strong></p>
<p>注: 后面代码使用pandas版本0.20.1,通过import pandas as pd引入</p>
<h3><strong>1. Series</strong></h3>
<p>Series是一种带有索引的序列对象。</p>
<p>简单创建如下:</p>
<pre class="blockcode"><code class="language-php"># 通过传入一个序列给pd.Series初始化一个Series对象, 比如list
s1=pd.Series(list("1234"))
print(s1)
0 1
1 2
2 3
3 4
dtype:object
</code></pre>
<h3><strong>2. DataFrame</strong></h3>
<p>类似与数据库table有行列的数据对象。</p>
<h4>创建方式如下:</h4>
<pre class="blockcode"><code class="language-php"># 通过传入一个numpy的二维数组或者dict对象给pd.DataFrame初始化一个DataFrame对象
# 通过numpy二维数组
import numpy as np
df1 = pd.DataFrame(np.random.randn(6,4))
print(df1)
0 1 2 3
0 -0.646340 -1.249943 0.393323 -1.561873
1 0.371630 0.069426 1.693097 0.907419
2 -0.328575 -0.256765 0.693798 -0.787343
3 1.875764 -0.416275 -1.028718 0.158259
4 1.644791 -1.321506 -0.33742
5 0.8206895 0.006391 -1.447894 0.506203 0.977295
# 通过dict字典
df2 = pd.DataFrame({ 'A' : 1.,
'B' : pd.Timestamp('20130102'),
'C' :pd.Series(1,index=list(range(4)),dtype='float32'),
'D' : np.array([3] * 4,dtype='int32'),
'E' : pd.Categorical(["test","train","test","train"]),
'F' : 'foo' })
print(df2)
A B C D E F
0 1.0 2013-01-02 1.0 3 test foo
1 1.0 2013-01-02 1.0 3 train foo
2 1.0 2013-01-02 1.0 3 test foo
3 1.0 2013-01-02 1.0 3 train foo
</code></pre>
<h3><strong>3. 索引</strong></h3>
<p>不管是Series对象还是DataFrame对象都有一个对对象相对应的索引,<strong>Series的索引类似于每个元素, DataFrame的索引对应着每一行。</strong></p>
<h4><strong>查看:</strong>在创建对象的时候,每个对象都会初始化一个起始值为0,自增的索引列表, DataFrame同理。</h4>
<pre class="blockcode"><code class="language-php"># 打印对象的时候,第一列就是索引
print(s1)
0 1
1 2
2 3
3 4
dtype: object
# 或者只查看索引, DataFrame同理
print(s1.index)
</code></pre>
<p style="text-align: left"><strong>二、</strong><strong>增删查改</strong></p>
<p>这里的增删查改主要基于<strong>DataFrame</strong>对象,为了有足够数据用于展示,这里选择<strong>tushare</strong>的数据。</p>
<p><strong>1. tushare安装</strong></p>
<pre class="blockcode"><code class="language-php">pip install tushare
</code></pre>
<p>创建数据对象如下:</p>
<pre class="blockcode"><code class="language-php">import tushare as ts
df = ts.get_k_data("000001")
</code></pre>
<p style="text-align: left">DataFrame 行列,axis 图解:<br></p>
<p style="text-align: center"><img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-584bd96be2ce94996a5d71aa75e5d78a"></p>
<p style="text-align: center"><img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-dd45a6d36f9b635e8e7c3e7f4b54e71d"></p>
<h3></h3>
<h3><br></h3>
<h3><strong>2. 查询</strong></h3>
<p>查看每列的数据类型</p>
<pre class="blockcode"><code class="language-php"># 查看df数据类型
df.dtypes
date object
open float64
close float64
high float64
low float64
volume float64
code object
dtype: object
</code></pre>
<p style="text-align: left">查看指定指定数量的行:head函数默认查看前5行,tail函数默认查看后5行,可以传递指定的数值用于查看指定行数。</p>
<pre class="blockcode"><code class="language-php">查看前5行
df.head()
date open close high low volume code
0 2015-12-23 9.927 9.935 10.174 9.871 1039018.0 000001
1 2015-12-24 9.919 9.823 9.998 9.744 640229.0 000001
2 2015-12-25 9.855 9.879 9.927 9.815 399845.0 000001
3 2015-12-28 9.895 9.537 9.919 9.537 822408.0 000001
4 2015-12-29 9.545 9.624 9.632 9.529 619802.0 000001
# 查看后5行
df.tail()
date open close high low volume code
636 2018-08-01 9.42 9.15 9.50 9.11 814081.0 000001
637 2018-08-02 9.13 8.94 9.15 8.88 931401.0 000001
638 2018-08-03 8.93 8.91 9.10 8.91 476546.0 000001
639 2018-08-06 8.94 8.94 9.11 8.89 554010.0 000001
640 2018-08-07 8.96 9.17 9.17 8.88 690423.0 000001
# 查看前10行
df.head(10)date open close high low volume code
0 2015-12-23 9.927 9.935 10.174 9.871 1039018.0 000001
1 2015-12-24 9.919 9.823 9.998 9.744 640229.0 000001
2 2015-12-25 9.855 9.879 9.927 |
|