zoukankan      html  css  js  c++  java
  • 数据分析三剑客之pandas

    在pandas中主要提供了两种数据类型,series与dataframe,前者相当于numpy中的一维数组,后者相当于一个excel或者.net中的datatable,即一个内存表,有了这样的数据结构进行数据分析即容易的多,可以通过python程序化处理达到类似于excel中的功能

    以下对series和dataframe的操作,分别介绍
    series:
    series的创建可以直接通过数组或者借用numpy创建的一维数组来创建series

    代码:

    import numpy as np
    import pandas
    from pandas import Series,DataFrame

    1:通过numpy数组创建

    arr1=np.arange(10)
    s1=Series(arr1)
    s1

    结果:
    0 0
    1 1
    2 2
    3 3
    4 4
    5 5
    6 6
    7 7
    8 8
    9 9
    dtype: int32

    为series设置索引

    s1.index=['A','B','C','D','E','F','H','I','J','K']
    s1
    结果:
    A 0
    B 1
    C 2
    D 3
    E 4
    F 5
    H 6
    I 7
    J 8
    K 9
    dtype: int32

    通过字典创建series

    dic1={
    'a':[1,2,3,4,5],
    'b':[6,7,8,9,10]
    }
    s2=Series(dic1)
    s2
    输出结果:
    a [1, 2, 3, 4, 5]
    b [6, 7, 8, 9, 10]
    dtype: object

    索引series数据:

    通过索引名称访问

    s1.A

    以字典方式取数据

    s1.get('D')

    索引自动对齐

    由于ss1中无D,ss2中无A,所以会会有两个索引无法匹配,生成NaN的结果
    ss1=Series([1,2,3])
    ss1.index=['A','B','C']
    ss2=Series([4,5,6])
    ss2.index=['B','C','D']
    ss1+ss2

    结果输出:
    A NaN
    B 6.0
    C 8.0
    D NaN
    dtype: float64

    创建Dataframe

    dataframe类似于excel的二维表格
    基于np.array创建
    arr1=np.array([100,120,70])
    s1=Series(arr1)

    设置索引

    s1.index=['语文','数学','英语']
    df1=DataFrame(s1)
    df1.columns=['成绩']
    df1

    输出:
    成绩
    语文 100
    数学 120
    英语 70
    基于随机数创建
    ata=np.random.randint(0,100,size=(5,5))
    data
    index=['小明','小黄','小红','小橙','小绿']
    columns=[['python','java','c','c++','c#']]
    df2=DataFrame(data=data,index=index,columns=columns)
    df2

    输出:
    python java c c++ c#
    小明 34 17 79 75 74
    小黄 20 44 79 34 20
    小红 74 13 51 49 33
    小橙 26 17 74 81 7
    小绿 92 34 61 8 89

    获取dataframe的数据

    获取列数据

    df2.python
    输出:
    python
    小明 34
    小黄 20
    小红 74
    小橙 26
    小绿 92

    按索引取

    df2.iloc[0]
    输出:
    python 74
    java 19
    c 4
    c++ 45
    c# 86
    Name: 小明, dtype: int32

    按索引名称取

    df2.loc['小明']

    输出:
    python 74
    java 19
    c 4
    c++ 45
    c# 86
    Name: 小明, dtype: int32

    按列取数据
    方式一:
    df2.get('python')
    方式二:
    df2.python
    输出:

    python
    小明 74
    小黄 91
    小红 26
    小橙 62
    小绿 24

    条件过滤

    选出python成绩大于70的记录
    dd=df2[df2.python>70].python
    dd
    输出:
    python
    小明 NaN
    小黄 NaN
    小红 74.0
    小橙 NaN
    小绿 92.0

    找出小黄的c#成绩

    df2.iloc[1]['c#']

    输出:
    c# 20
    Name: 小黄, dtype: int32

    找出小黄的c#成绩

    df2.loc['小黄'].iloc[4]

    输出:
    20

    查看所有的index,value
    查看df2的所有的value:df2.value
    输出:
    array([[34, 17, 79, 75, 74],
    [20, 44, 79, 34, 20],
    [74, 13, 51, 49, 33],
    [26, 17, 74, 81, 7],
    [92, 34, 61, 8, 89]])

    df2的所有的index :df2.index
    输出:
    Index(['小明', '小黄', '小红', '小橙', '小绿'], dtype='object')

    查看所有列
    查看df2中的所有的列
    df2.columns
    输出:
    MultiIndex(levels=[['c', 'c#', 'c++', 'java', 'python']],
    labels=[[4, 3, 0, 2, 1]])
    查看dateframe的维度
    df2.shape

    按索引截取dataframe数据
    df2['小明':'小红']

    输出:
    python java c c++ c#
    小明 34 17 79 75 74
    小黄 20 44 79 34 20
    小红 74 13 51 49 33

    取索引为小明到小红的python与c的成绩
    df2['小明':'小红'][['python','c']]

    输出

    python c
    小明 34 79
    小黄 20 79
    小红 74 51

    条件查询

    df2[(df2['python']>40) |(df2['python']<30 )]['python']

    输出:
    python
    小明 NaN
    小黄 20.0
    小红 74.0
    小橙 26.0
    小绿 92.0

    求所有人中python成绩最低分
    df2.python.min()
    求python平均分
    df2.python.mean()
    取python的最高成绩
    df2.python.max()
    计算python成绩标准差
    df2.python.std()
    计算python成绩方差
    df2.python.var()

    连接数据库
    import MySQLdb
    conn= MySQLdb.connect(
    host='localhost',
    port = 3306,
    user='root',
    passwd='434944800',
    db ='butterfly_record'
    )
    sql='select * from labelinfo'
    data=pandas.read_sql_query(sql,conn)
    data.head()

    输出:

    id labelId labelName short_labelName brief icon label_background_image label_page parentLabelId
    0 1 81205 ???? ???? None None None None 2.0
    1 2 81202 ????? ????? None None None None 2.0
    2 3 81201 ??? ??? None None None None 2.0
    3 4 81200 ?? ?? None None None None NaN
    4 5 81204 ???? ???? None None None None 2.0

    按条件过滤

    data.query('labelId=="81205"')
    输出:

    id labelId labelName short_labelName brief icon label_background_image label_page parentLabelId
    0 1 81205 ???? ???? None None None None 2.0
    删除名称为labelName的列

    noname=data.drop(['labelName'],axis=1)
    noname
    输出:
    id labelId short_labelName brief icon label_background_image label_page parentLabelId
    0 1 81205 ???? None None None None 2.0
    1 2 81202 ????? None None None None 2.0
    2 3 81201 ??? None None None None 2.0
    3 4 81200 ?? None None None None NaN
    4 5 81204 ???? None None None None 2.0
    5 6 81206 ????? None None None None 2.0
    6 7 1 ?? None None None None NaN
    7 8 1001 None None None None None 1.0
    8 9 1002 None None None None None 1.0
    9 10 1005 None None None None None 1.0
    10 11 1006 None None None None None 1.0
    11 12 1007 None None None None None 1.0
    12 13 1012 None None None None None 1.0
    13 14 1028 None None None None None 1.0
    14 15 1030 None None None None None 1.0
    15 16 1031 None None None None None 1.0
    16 17 1032 None None None None None 1.0
    17 18 1033 None None None None None 1.0
    18 19 1036 None None None None None 1.0
    19 20 1036 None None None None None 1.0
    20 21 1110 None None None None None 1.0
    21 22 1111 None None None None None NaN
    22 28 2 vip None None None None NaN
    23 29 81207 None None None None None 2.0
    24 30 81134 None None None None None 2.0
    25 31 81137 None None None None None 2.0
    删除索引为0的行
    data.drop([0],axis=0)

    修改数据,并更新到原dataframe
    data.drop([4],axis=0,inplace=True)
    data

  • 相关阅读:
    显示/隐藏Mac下的隐藏文件
    遍历指定文件下所有文件,删除指定后缀文件
    ssh公钥
    设置状态栏(statusbar)的样式
    Silverlight上传下载三种方法解析(三)
    Silverlight上传下载三种方式解析(二)
    Silverlight 上传下载之三种方式解析
    一个简单的存储过程代码生成器
    .net 程序发生了一个不可捕获的异常
    n取的r的组合数问题
  • 原文地址:https://www.cnblogs.com/lijintian/p/12850731.html
Copyright © 2011-2022 走看看