zoukankan      html  css  js  c++  java
  • 数据分析与展示--pandas基础

    pandas提供了

    1.便于操作的数据类型

    2.提供了很多分析函数和分析工具

    import pandas as pd
    d = pd.Series(range(20))
    # print(d)
    print(d.cumsum())

    padas库的理解

    两个数据类型:Series(一维),DataFrame(多维)

    关注数据的应用表达,数据和索引之间的关系

    series:理解为带标签数组

    可以通过标量创建

    s = pd.Series(5,index=['x','y','z'])
    print(s)

    可以通过字典创建

    s = pd.Series({'x':1,'y':2,'z':3})
    print(s)
    s = pd.Series({'x':1,'y':2,'z':3},index=['z','s','c','x','y'])
    print(s)

    可以通过ndarrary创建

    import pandas as pd
    import numpy as np
    b = pd.Series(np.arange(1,10))
    print(b)
    import pandas as pd
    import numpy as np
    b = pd.Series(np.arange(1,4),index=np.arange(9,4,-2))
    print(b)

    方法

    .index   获得索引

    .values 获得数据

    import pandas as pd
    import numpy as np
    b = pd.Series(np.arange(1,4),index=np.arange(9,4,-2))
    print(b.index)

    由此可以得知Series类型就是index类型+ndarray类型

    s = pd.Series({'x':1,'y':2,'z':3},index=['z','s','c','x','y'])
    # print(s)
    print(s['x'])
    print(s[0])  #此数可以得知数字索引是默认生产的
    #两套索引可以并存但是不可以混用

    可以使用ndarray的方法以及切片

    s = pd.Series({'x':1,'y':2,'z':3},index=['z','s','c','x','y'])
    print(s[s>s.median()])
    print(np.exp(s))
    print(s[:3])

    可以使用字典中的in和.get方法

    in判断的是:是不是索引的一部分

    .get(a,b)若存在索引a返回值,不存在返回b

    Series+Series:索引对应的值相加,索引自动对齐,如果索引只存在一个Series里相加得到的是NaN

    a = pd.Series([1,2,3],['a','b','c'])
    b = pd.Series([4,5,6],['b','c','d'])
    c = a+b
    print(c) 
    
    #a    NaN
    #b    6.0
    #c    8.0
    #d    NaN
    #dtype: float64

    属性:

    a.name可以存贮在Series中

    Series可以随时更改,即时生效

    DataFrame:二维的带标签的数组

    多列数据共用同一索引

    dateframe是一个表格型的数据类型,每列值类型可以不同

    dateframe既有行索引也有列索引

    dateframe常用来表达二维数据,也可以用来表达多维数据

    可以从以下创建

    二维ndarrary

    由一维ndarrary,列表,字典,元组或Series构成的字典    注:字典的键是列索引,值是行索引

    Series类型

    其他的DateFrame类型

    import pandas as pd
    dt = {
        'one':pd.Series([1,2,3],index=['a','b','c']),
        'two':pd.Series([4,5,6,7],index=['a','b','c','d'])
    }
    d = pd.DataFrame(dt)
    print(d)
    d = pd.DataFrame(dt,index=['b','c','d'],columns=['two','three'])
    print(d)
    print(d.index)
    print(d.columns)
    print(d.values)

     

    索引

    Series和DateFrame的索引是是index类型

    index对象是不可修改类型

    改变Series和DateFrame

    reindex(index= [] , columns =[])

    reindex的其他参数

    fill_value  填充缺失位置的值

    method 填充方法,ffill当前值向前填充,bfill向后填充

    limit    最大填充量

    copy  默认为False,生产新对象,为True时,新旧相等不复制

    import pandas as pd
    d1 = {
        '城市':['北京','上海','广州','深圳','沈阳'],
        '环比':[101.5,101.2,101.3,102.0,100.1],
        '同比':[121.5,111.2,121.3,102.0,110.1],
        '定基':[131.5,131.2,111.3,152.0,170.1],
    }
    d = pd.DataFrame(d1,index=['c1','c2','c3','c4','c5'])
    print(d)
    print(d['城市'])      #列    index   axis=0
    print(d.ix['c2'])     #行    column  axis=1
    print(d['城市']['c2'])
    d = d.reindex(index = ['c3','c4','c5','c1','c2'])
    print(d)
    d = d.reindex(columns = ['城市','同比','定基','环比'])
    print(d)
    newc = d.columns.insert(1,'均值')
    newd = d.reindex(columns = newc , fill_value=200)
    print(newc)
    print(newd)

    d.index和d.columns的常用方法

    .append(inx)  连接另一个index对象产生新的index对象

    .diff(inx)    计算差集,产生新的index对象

    .intersection(inx)计算交集

    .union(idx)     计算并集

    .delete(loc)    删除loc位置处的元素

    .insert(loc,e)  在loc位置处增加一个元素

    d.drop(['索引1','索引2])

    import pandas as pd
    d1 = {
        '城市':['北京','上海','广州','深圳','沈阳'],
        '环比':[101.5,101.2,101.3,102.0,100.1],
        '同比':[121.5,111.2,121.3,102.0,110.1],
        '定基':[131.5,131.2,111.3,152.0,170.1],
    }
    d = pd.DataFrame(d1,index=['c1','c2','c3','c4','c5'])
    # print(d)
    nc = d.columns.delete(2)
    ni = d.index.insert(5,'c6')
    nd = d.reindex(index=ni,columns=nc).ffill()
    print(nd)
    nd = nd.drop(['c1','c3'])
    print(nd)
    nd = nd.drop(['定基'],axis=1)
    print(nd)

    运算

    运算规则

    1.算数运算根据行列索引补齐后运算,运算默认产生浮点数

    2.补齐时,缺项填充NaN

    3.二维和一维,一维和零维之间为广播运算

    4.采用+-*/符号进行的二元运算产生新的对象

    +  .add

    -    .sub

    *   .mul

    /   .dev

    可以使用参数(例如fill_value,axis)

    广播运算

    低维的与高维的每一个值产生运算

    例如当一维和二维运算时,默认与axis=1轴进行运算,可以使用函数改变默认值

    也可以进行bool值运算

    import pandas as pd
    import numpy as np
    b = pd.DataFrame(np.arange(20).reshape(4,5))
    c = pd.Series(np.arange(4))
    b = pd.DataFrame(np.arange(20).reshape(4,5))
    d = pd.DataFrame(np.arange(20,0,-1).reshape(4,5))
    print(b-c)
    print(b.sub(c,axis=0))
    print(b == d)
    print(b >= d)
    print(c >0)

    排序与统计

    b.sortindex(axis=0,ascending=True)

    b.sortvalue(by,axis=0,ascending=True)

    by是排序的轴

    在指定轴上对索引进行排序,默认为0轴升序

    NaN放在排序的末尾

    Series和DateFrame的方法

    .sum()      0轴计算和

    .sount()      非NaN值的数量

    .mean()   算数平均值

    .median()   算数中位数

    .var()     方差

    .std()     标准差

    .min()    最小值

    .max()   最大值

    Series可用的

    .argmin()  .argmax()  返回最大值和最小值的自动索引

    .idxmin()   .idxmax()  返回最大值和最小值的自定义索引

    万能方法Series

    .describe()  针对0轴的统计汇总

    输出一个Series类型的统计列表

    import pandas as pd
    import numpy as np
    b = pd.DataFrame(100*np.random.randn(20).reshape(4,5),dtype=int)
    print(b.describe()[0])
    print(b.describe().ix['count'])

    累计统计分析函数

    .cumsum()  依次给出前1,2,...n个数的和

    .cumpord()  依次给出前1,2,...n个数的积

    .cummax()  依次给出前1,2,...n个数的最大值

    .cummin()   依次给出前1,2,...n个数的最小值

    滚动计算(窗口计算)

    .rolling(w).sum()     依次计算相邻w个元素的和

    .rolling(w).mean()     依次计算相邻w个元素的算数平均值

    .rolling(w).var()    依次计算相邻w个元素的方差

    .rolling(w).std()    依次计算相邻w个元素的标准差

    .rolling(w).min().max() 依次计算相邻w个元素的最小值和最大值

    import pandas as pd
    import numpy as np
    b = pd.DataFrame(10*np.random.randn(20).reshape(4,5),dtype=int)
    print(b)
    print(b.cumsum())
    print(b.rolling(3).mean())

    相关性

    协方差可以大致表示相关性

    pearson相关系数

    .cov()  协方差

    .corr()  相关系数矩阵,Pearson,Spearman,Kendall等系数,默认使用pearson方法。

  • 相关阅读:
    Liunx运维(七)-用户管理及用户信息查询命令
    容器网络原理分析:veth 和 network namespace
    浅谈 Docker 网络:单节点多容器
    浅谈 Docker 网络:单节点单容器
    图图图
    LinkPrediction可能有用的数据集
    2021年展望
    2020年总结
    毕业设计:基于web的外卖订餐系统的设计与实现
    机器学习和数据分析在实体识别方面的简单应用
  • 原文地址:https://www.cnblogs.com/zsc329/p/9374842.html
Copyright © 2011-2022 走看看