码迷,mamicode.com
首页 > 其他好文 > 详细

pandas.DataFrame学习系列1——定义及属性

时间:2017-11-01 14:58:31      阅读:548      评论:0      收藏:0      [点我收藏+]

标签:整数   port   block   南京   range   run   imei   records   lock   

定义:

DataFrame是二维的、大小可变的、成分混合的、具有标签化坐标轴(行和列)的表数据结构。基于行和列标签进行计算。可以被看作是为序列对象(Series)提供的类似字典的一个容器,是pandas中主要的数据结构。

形式:

class pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)

参数含义:

data : numpy ndarray(多维数组)(结构化或同质化的), dict(字典), or DataFrame(数据表)如果是字典类型,该字典可以包含序列,数组,常量或类列表型对象
index : Index or array-like 索引或数组类型,如果输入数据中没有索引信息以及没有提供索引时默认赋值为arange(n)即从0开始的等差数组
columns : Index
or array-like 索引或数组类型,没有提供列标签时默认赋值为0开始的等差数组
dtype : dtype, default None 数据类型,默认为空。
只允许有一种数据类型,如果为空,自动推断类型
copy : boolean, default False 布尔类型,默认为False。
从输入值中拷贝数据,只对输入为DataFrame或者二维数组时有影响

其他构建DataFrame类型的方法:

classmethod DataFrame.from_records(data, index=None, exclude=None, columns=None, coerce_float=False, nrows=None)[source]

classmethod DataFrame.from_dict(data, orient=columns, dtype=None)

pandas.read_csv, pandas.read_table, pandas.read_clipboard,pandas.read_excel等

 举例:

从字典构建DataFrame
>>> d = {col1: [1, 2], col2: [3, 4]} >>> df = pd.DataFrame(data=d) >>> df col1 col2 0 1 3 1 2 4
推断类型为int64
>>> df.dtypes col1 int64 col2 int64 dtype: object
强制设置为单一类型
>>> df = pd.DataFrame(data=d, dtype=np.int8)
>>> df.dtypes
col1    int8
col2    int8
dtype: object
从numpy多维数组类型构建DataFrame
>>> df2 = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 5)),
...                    columns=[a, b, c, d, e])
>>> df2
    a   b   c   d   e
0   2   8   8   3   4
1   4   2   9   0   9
2   1   0   7   8   0
3   5   1   7   1   3
4   6   0   2   4   2

属性:

获取和创建DataFrame

 1 import pandas as pd
 2 import numpy as np
 3  
 4 df=pd.read_excel(南京银行.xlsx,index_col=Date)
 5 df1=df[:5]
 6 
 7 In [38]:df1.head()
 8 Out[38]: 
 9          Open  High   Low  Close  Trunover    Volume
10 Date                                                   
11 2017-09-15  8.06  8.08  8.03   8.04    195.43  24272800
12 2017-09-18  8.05  8.13  8.03   8.06    200.76  24867600
13 2017-09-19  8.03  8.06  7.94   8.00    433.76  54253100
14 2017-09-20  7.97  8.06  7.95   8.03    319.94  39909700
15 2017-09-21  8.02  8.10  7.99   8.04    241.94  30056600

--转置,转置以后DataFrame的index属性值为None

1 In [39]:  df1.T
2 Out[39]: 
3 Date       2017-09-15   2017-09-18   2017-09-19   2017-09-20   2017-09-21
4 Open             8.06         8.05         8.03         7.97         8.02
5 High             8.08         8.13         8.06         8.06         8.10
6 Low              8.03         8.03         7.94         7.95         7.99
7 Close            8.04         8.06         8.00         8.03         8.04
8 Trunover       195.43       200.76       433.76       319.94       241.94
9 Volume    24272800.00  24867600.00  54253100.00  39909700.00  30056600.00

--基于标签快速访问

In [35]:  date=pd.to_datetime(2017-09-15)
In [36]:  date
Out[36]: Timestamp(2017-09-15 00:00:00)
In [37]:  df1.at[date,Open]
Out[37]: 8.0600000000000005

--获取行轴和列轴标签名

1 In [44]: df1.axes
2 Out[44]: 
3 [DatetimeIndex([2017-09-15, 2017-09-18, 2017-09-19, 2017-09-20,
4                 2017-09-21], dtype=datetime64[ns], name=Date, freq=None),
5  Index([Open, High, Low, Close, Trunover, Volume], dtype=object)]

 --内置属性

 1 In[45]: df1.blocks
 2 Out[45]: 
 3 {float64:     Open  High   Low  Close  Trunover
 4  Date                                         
 5  2017-09-15  8.06  8.08  8.03   8.04    195.43
 6  2017-09-18  8.05  8.13  8.03   8.06    200.76
 7  2017-09-19  8.03  8.06  7.94   8.00    433.76
 8  2017-09-20  7.97  8.06  7.95   8.03    319.94
 9  2017-09-21  8.02  8.10  7.99   8.04    241.94,
10  int64:               Volume
11  Date                
12  2017-09-15  24272800
13  2017-09-18  24867600
14  2017-09-19  54253100
15  2017-09-20  39909700
16  2017-09-21  30056600}

--各列数据类型

1 In[46]:  df1.dtypes
2 Out[46]: 
3 Open        float64
4 High        float64
5 Low         float64
6 Close       float64
7 Trunover    float64
8 Volume        int64
9 dtype: object

 --判断DataFrame是否完全为空

1 In [47]: df1.empty
2 Out[47]: False

--返回稀疏或密集的标示及数据类型

1 In[48]:  df1.ftypes
2 Out[48]: 
3 Open        float64:dense
4 High        float64:dense
5 Low         float64:dense
6 Close       float64:dense
7 Trunover    float64:dense
8 Volume        int64:dense
9 dtype: object

--快速整数标量定位(到具体元素,相当于给出坐标)

1 In[49]:  df1.iat[0,1] #第1行,第2列
2 Out[49]: 8.0800000000000001
3 
4 In[50]:  df1.iat[1,0] #第2行,第1列
5 Out[50]: 8.0500000000000007

 --用于位置选择的基于整数定位的索引(切片)

 1 In [2]:  df1.iloc[0:1]
 2 Out[2]: 
 3             Open  High   Low  Close  Trunover    Volume
 4 Date                                                   
 5 2017-09-15  8.06  8.08  8.03   8.04    195.43  24272800
1 In [3]:  df1.iloc[0:1,2:]
2 Out[3]: 
3              Low  Close  Trunover    Volume
4 Date                                       
5 2017-09-15  8.03   8.04    195.43  24272800

--混合方式定位(基于整数位置或标签名以及它们的组合,可以只用行标签,但是不能只用列标签)

1 In [6]:  df1.ix[1,Open]
2 Out[6]: 8.0500000000000007
1 In [7]:  df1.ix[1]
2 Out[7]: 
3 Open               8.05
4 High               8.13
5 Low                8.03
6 Close              8.06
7 Trunover         200.76
8 Volume      24867600.00
9 Name: 2017-09-18 00:00:00, dtype: float64

--选择位置的基于标签名的索引

1 In[7]:  df1.loc[date,Low]
2 Out[7]: 8.0299999999999994
3 
4 In [8]: df1.loc[df1.index[0],Low]
5 Out[8]: 8.0299999999999994

--坐标轴个数

1 In [10]: df1.ndim
2 Out[10]: 2

--DataFrame的形状(行列数)

1 In [11]:  df1.shape
2 Out[11]: (5, 6)

--DataFrame的大小(元素个数)

1 In [12]:  df1.size
2 Out[12]: 30

--返回DataFrame样式对象

1 In [13]:  df1.style
2 Out[13]: <pandas.io.formats.style.Styler at 0x1c410cf8eb8>

--返回DataFrame里的数值(二维数组)

 1 In [14]: df1.values
 2 Out[14]: 
 3 array([[  8.06000000e+00,   8.08000000e+00,   8.03000000e+00,
 4           8.04000000e+00,   1.95430000e+02,   2.42728000e+07],
 5        [  8.05000000e+00,   8.13000000e+00,   8.03000000e+00,
 6           8.06000000e+00,   2.00760000e+02,   2.48676000e+07],
 7        [  8.03000000e+00,   8.06000000e+00,   7.94000000e+00,
 8           8.00000000e+00,   4.33760000e+02,   5.42531000e+07],
 9        [  7.97000000e+00,   8.06000000e+00,   7.95000000e+00,
10           8.03000000e+00,   3.19940000e+02,   3.99097000e+07],
11        [  8.02000000e+00,   8.10000000e+00,   7.99000000e+00,
12           8.04000000e+00,   2.41940000e+02,   3.00566000e+07]])

以上为DataFrame的主要属性,后面继续介绍DataFrame的方法。

 

pandas.DataFrame学习系列1——定义及属性

标签:整数   port   block   南京   range   run   imei   records   lock   

原文地址:http://www.cnblogs.com/snow-Andy/p/7762151.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!