Python pandas入门笔记

http://blog.csdn.net/qq_16234613/article/details/62046057?ticket=ST-6265-gxbpNZgfKckiXDsUIYZq-passport.csdn.net
学习以上内容并摘出部分记录
Pandas的主要数据结构:
DimensionsNameDescription
1Series1D labeled homogeneously-typed array
2DataFrameGeneral 2D labeled, size-mutable tabular structure with potentially heterogeneously-typed columns
3Panel

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

>>> 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)

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
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值