zoukankan      html  css  js  c++  java
  • pandas入门之Series、DataFrame和Index

    (一)Series

    可以把 Pandas 的 Series 对象看成一种特殊的 Python 字典 “{ }”, 将类型键映射到类型值.(显式索引)。

    data.values , data.index

    1 data = pd.Series([0.25, 0.5, 0.75, 1.0], index=['a', 'b', 'c', 'd'])  #创建pd.Series对象,**这是由字典列表创建**
    2 
    3 #data:  
    4 a 0.25
    5 b 0.50
    6 c 0.75
    7 d 1.00

    Series对象支持切片操作。

    1 data["a":"v"]
    2 
    3 #Output:
    4 a    1
    5 b    2
    6 v    3

    (二)Index
    0 : 类似数组一样的操作去获取数值,也可以切片。
    1:不能通过index索引改变数组的数值。
    2 : 可以实现一些pandas对象的集合操作,并、交、差。
    3:注意显式索引做切片时包括最后一个索引,隐式索引切片不包括最后一个索引。

    索引器-indexer:可以暴露切片接口的属性,避免混乱, loc , iloc , ix

    (三)DataFrame

    DataFrame 可以看作一种通用的 NumPy 二维数组size(a,b),相当于多个Series组成一个Series是一个列。 它的行与列都可以通过索引获取。
    pd.DataFrame().index #索引标签,输出相当于EXCEL最左侧一列。
    pd.DataFrame().collums #返回一个存放列标签的index对象。
    缺失值会用NaN表示:Not a number.

    创建pd.DataFrame:几种方法
    0 : 通过单个 Series 对象创建
    1 : 字典列表
    2:通过Series对象字典
    3:通过numpy二维数组创建
    4 : 通过 NumPy 结构化数组创建

     1 pd.DataFrame(population, columns=['population'])     #way0
     2 #Output:                               population
     3                  California           38332521
     4                  Florida              19552860
     5                  Illinois             12882135
     6                  New York             19651127
     7                  Texas                26448193
     8 
     9 
    10 data = [{'a': i, 'b': 2 * i}for i in range(3)]        #way1
    11 pd.DataFrame(data)
    12 #Out[24]:     
    13                           a b
    14             0 0 0
    15             1 1 2
    16             2 2 4 
    17              
    18 
    19 pd.DataFrame({'population': population,'area': area})    #way2  
    20 #Out[24]:              
    21                            area population
    22                        California 423967 38332521
    23             Florida   170312 19552860
    24             Illinois  149995 12882135
    25             New York  141297 19651127
    26             Texas     695662 26448193
    27 
    28 
    29 pd.DataFrame(np.random.rand(3, 2),columns=['foo', 'bar'], index=['a', 'b', 'c'])  #way3  
    30 #Out[27]:    
    31                     foo      bar
    32         a 0.865257 0.213169
    33         b 0.442759 0.108267
    34         c 0.047110 0.905718
    35 
    36 
    37 A = np.zeros(3, dtype=[('A', 'i8'), ('B', 'f8')])          #way4
    38 pd.DataFrame(A)
    39 #Out[29]: 
    40                   A   B
    41         0  0  0.0
    42         1  0  0.0
    43         2  0  0.0                            
  • 相关阅读:
    数据库事务的四个隔离级别浅析
    Spring事务管理之几种方式实现事务
    SQL调优简介及调优方式
    Spring MVC工作流程图
    java中的垃圾回收机
    iOS 本地化-国际化-(App名字国际化)
    iOS-自建iPa应用分发平台
    稳定App缓存
    iOS-保存图片到(自定义)相册步骤
    iOS -根据网络状态加载图片
  • 原文地址:https://www.cnblogs.com/Henry-ZHAO/p/12725349.html
Copyright © 2011-2022 走看看