zoukankan      html  css  js  c++  java
  • Pandas基本介绍

    1、pandas主要的两个数据结构:Series和DataFrame

    Series的字符串表现形式为:索引在左边,值在右边。由于我们没有为数据指定索引。于是会自动创建一个0到N-1(N为长度)的整数型索引。

    >>> import pandas as pd
    >>> import numpy as np
    >>> s = pd.Series([1,3,6,np.nan,44,1])
    >>> print(s)
    0     1.0
    1     3.0
    2     6.0
    3     NaN
    4    44.0
    5     1.0
    dtype: float64

     DataFrame是一个表格型的数据结构,它包含有一组有序的列,每列可以是不同的值类型(数值,字符串,布尔值等)。DataFrame既有行索引也有列索引, 它可以被看做由Series组成的大字典。

    >>> dates = pd.date_range('20160101',periods=6)
    >>> df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=['a','b','c','d'])
    >>> print(df)
                       a         b         c         d
    2016-01-01  1.306762  1.506943  0.682025 -0.054329
    2016-01-02  2.626875  0.086998  0.307123 -0.498728
    2016-01-03 -0.941697  0.206144  1.719719  1.084614
    2016-01-04 -0.610912 -1.120358 -0.635338  1.145777
    2016-01-05 -0.150501  0.768586 -0.158341  0.704960
    2016-01-06 -0.759211  0.271800  0.768166 -0.293015

    2、DataFrame的一些简单运用

    >>> print(df['b'])
    2016-01-01    1.506943
    2016-01-02    0.086998
    2016-01-03    0.206144
    2016-01-04   -1.120358
    2016-01-05    0.768586
    2016-01-06    0.271800
    Freq: D, Name: b, dtype: float64
    
    >>> df1 = pd.DataFrame(np.arange(12).reshape((3,4)))#创建一组没有给定行标签和列标签的数据 df1:
    >>> print(df1)
       0  1   2   3
    0  0  1   2   3
    1  4  5   6   7
    2  8  9  10  11
    
    >>> 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'})#另一种生成df的方法
    >>>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
    
    >>> print(df2.dtypes)#查看数据中的类型
    A           float64
    B    datetime64[ns]
    C           float32
    D             int32
    E          category
    F            object
    dtype: object
    
    >>> print(df2.index) #查看对列的序号
    Int64Index([0, 1, 2, 3], dtype='int64')
    
    >>> print(df2.columns)#查看每种数据的名称
    Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')
    
    >>> print(df2.values)#查看所有df2的值
    [[1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'test' 'foo']
     [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'train' 'foo']
     [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'test' 'foo']
     [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'train' 'foo']]
    
    >>> df2.describe()#数据的总结
             A    C    D
    count  4.0  4.0  4.0
    mean   1.0  1.0  3.0
    std    0.0  0.0  0.0
    min    1.0  1.0  3.0
    25%    1.0  1.0  3.0
    50%    1.0  1.0  3.0
    75%    1.0  1.0  3.0
    max    1.0  1.0  3.0
    
    >>> print(df2.T)#翻转数据,transpose
                         0                    1                    2                    3
    A                    1                    1                    1                    1
    B  2013-01-02 00:00:00  2013-01-02 00:00:00  2013-01-02 00:00:00  2013-01-02 00:00:00
    C                    1                    1                    1                    1
    D                    3                    3                    3                    3
    E                 test                train                 test                train
    F                  foo                  foo                  foo                  foo
    
    >>> print(df2.sort_index(axis=1, ascending=False))#对数据的index进行排序并输出
         F      E  D    C          B    A
    0  foo   test  3  1.0 2013-01-02  1.0
    1  foo  train  3  1.0 2013-01-02  1.0
    2  foo   test  3  1.0 2013-01-02  1.0
    3  foo  train  3  1.0 2013-01-02  1.0
    
    >>> print(df2.sort_values(by='B'))#对数据值排序输出
         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
  • 相关阅读:
    Codeforces Round #256 (Div. 2/B)/Codeforces448B_Suffix Structures(字符串处理)
    【android】优秀的UI资源站点集合
    升级iOS8系统后,保险箱Pro、私人保险箱、私密相冊打开就闪退的官方解决方式
    js产生随机数
    java实现各种数据统计图(柱形图,饼图,折线图)
    Matlab画图-非常具体,非常全面
    Lucene教程具体解释
    NAND FLASH
    Jenkins(二)
    iOS 本地通知
  • 原文地址:https://www.cnblogs.com/anhoo/p/9383664.html
Copyright © 2011-2022 走看看