http://blog.youkuaiyun.com/qq_16234613/article/details/62046057?ticket=ST-6265-gxbpNZgfKckiXDsUIYZq-passport.youkuaiyun.com
学习以上内容并摘出部分记录
Pandas的主要数据结构:
Dimensions | Name | Description |
---|---|---|
1 | Series | 1D labeled homogeneously-typed array |
2 | DataFrame | General 2D labeled, size-mutable tabular structure with potentially heterogeneously-typed columns |
3 | Panel |
General 3D labeled, also size-mutable array
|
import pandas
as
pd
字典对象
pd.Series(data,index)
先看看官网参数解释如下,学一个简单赋值案例,操作跟
| Parameters
| ----------
| data : array-like, dict, or scalar value
| Contains data stored in Series
| index : array-like or Index (1d)
| Values must be hashable and have the same length as `data`.
| Non-unique index values are allowed. Will default to
| RangeIndex(len(data)) if not provided. If both a dict and index
| sequence are used, the index will override the keys found in the
| dict.
| dtype : numpy.dtype or None
| If None, dtype will be inferred
| copy : boolean, default False
| Copy input data
| ----------
| data : array-like, dict, or scalar value
| Contains data stored in Series
| index : array-like or Index (1d)
| Values must be hashable and have the same length as `data`.
| Non-unique index values are allowed. Will default to
| RangeIndex(len(data)) if not provided. If both a dict and index
| sequence are used, the index will override the keys found in the
| dict.
| dtype : numpy.dtype or None
| If None, dtype will be inferred
| copy : boolean, default False
| Copy input data
>>> s = pd.Series(data=[
1
,
2
,
3
,
4
],
index
= [
'a'
,
'b'
,
'c'
,
'd'
])
//传入键和值方式
>>> s
a 1
b 2
c 3
d 4
dtype: int64
>>> s. index //获取键列表
Index ([ 'a' , 'b' , 'c' , 'd' ], dtype= 'object' )
>>> s.values //获取值列表
array ([ 1 , 2 , 3 , 4 ], dtype=int64)
>>> s
a 1
b 2
c 3
d 4
dtype: int64
>>> s. index //获取键列表
Index ([ 'a' , 'b' , 'c' , 'd' ], dtype= 'object' )
>>> s.values //获取值列表
array ([ 1 , 2 , 3 , 4 ], dtype=int64)
DataFrame表格对象
复制一下案例以后参考使用
In [
10
]: df2 = pd.DataFrame({
'A'
:
1.
,
'B' : pd.Timestamp( '20130102' ),
'C' : pd.Series( 1 ,index= list (range( 4 )),dtype= 'float32' ), //生成Series对象,取的是value
'D' : np. array ([ 3 ] * 4 ,dtype= 'int32' ), //生成numpy对象
'E' : pd.Categorical([ "test" , "train" , "test" , "train" ]), 'F' : 'foo' })
In [ 11 ]: df2
Out[ 11 ]: // 默认以数字从0开始作为行键,以字典键为列键
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
'B' : pd.Timestamp( '20130102' ),
'C' : pd.Series( 1 ,index= list (range( 4 )),dtype= 'float32' ), //生成Series对象,取的是value
'D' : np. array ([ 3 ] * 4 ,dtype= 'int32' ), //生成numpy对象
'E' : pd.Categorical([ "test" , "train" , "test" , "train" ]), 'F' : 'foo' })
In [ 11 ]: df2
Out[ 11 ]: // 默认以数字从0开始作为行键,以字典键为列键
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
In
[
6
]: dates = pd.date_range(
'20130101'
, periods=
6
)
In [ 7 ]: dates
Out [ 7 ]:
DatetimeIndex([ '2013-01-01' , '2013-01-02' , '2013-01-03' , '2013-01-04' ,
'2013-01-05' , '2013-01-06' ],
dtype= 'datetime64[ns]' , freq= 'D' )
In [ 8 ]: df = pd.DataFrame(np.random.randn( 6 , 4 ), index =dates, columns=list( 'ABCD' )) //np.random.randn(6,4)返回一个样本,具有标准正态分布
In [ 9 ]: df
Out [ 9 ]: // 指定dates为行键,columns为列键
A B C D
2013 - 01 - 01 0.469112 - 0.282863 - 1.509059 - 1.135632
2013 - 01 - 02 1.212112 - 0.173215 0.119209 - 1.044236
2013 - 01 - 03 - 0.861849 - 2.104569 - 0.494929 1.071804
2013 - 01 - 04 0.721555 - 0.706771 - 1.039575 0.271860
2013 - 01 - 05 - 0.424972 0.567020 0.276232 - 1.087401
2013 - 01 - 06 - 0.673690 0.113648 - 1.478427 0.524988
In [ 7 ]: dates
Out [ 7 ]:
DatetimeIndex([ '2013-01-01' , '2013-01-02' , '2013-01-03' , '2013-01-04' ,
'2013-01-05' , '2013-01-06' ],
dtype= 'datetime64[ns]' , freq= 'D' )
In [ 8 ]: df = pd.DataFrame(np.random.randn( 6 , 4 ), index =dates, columns=list( 'ABCD' )) //np.random.randn(6,4)返回一个样本,具有标准正态分布
In [ 9 ]: df
Out [ 9 ]: // 指定dates为行键,columns为列键
A B C D
2013 - 01 - 01 0.469112 - 0.282863 - 1.509059 - 1.135632
2013 - 01 - 02 1.212112 - 0.173215 0.119209 - 1.044236
2013 - 01 - 03 - 0.861849 - 2.104569 - 0.494929 1.071804
2013 - 01 - 04 0.721555 - 0.706771 - 1.039575 0.271860
2013 - 01 - 05 - 0.424972 0.567020 0.276232 - 1.087401
2013 - 01 - 06 - 0.673690 0.113648 - 1.478427 0.524988