zoukankan      html  css  js  c++  java
  • 二、Pandas基础:DataFrame

    一、DataFrame简介

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

    二、DataFrame创建

    • DataFrame可以使用数组,列表和字典等方式进行创建
    • 当DataFrame数组和列表创建时,可指定参数index(行索引)和columns(列索引),如果不指定,默认用0的列表为序列号
    • 创建后的DataFrame,亦可修改行列参数
    import pandas as pd
    a = pd.DataFrame(
        [
            ['小红', '小花', '小兰'],
            [85, 93, 99]
        ])
    b = pd.DataFrame(
        [
            ['小红', 85],
            ['小花', 93],
            ['小兰', 99]
        ], columns=['姓名', '成绩']
    )
    data = {
        'apart': ['101', '102', '103', '104'],
        'profits': [681.5, 125, 15.5, 160],
        'year': [2007, 2010, 2012, 2008],
        'months': 8
    }
    c = pd.DataFrame(data, index=['one', 'two', 'three', 'four'])
    print("普通列表创建:
    ", a)
    print("带列名的创建:
    ", b)
    print("字典方式创建:
    ", c)
    >>>
    普通列表创建:
         0   1   2
    0  小红  小花  小兰
    1  85  93  99
    带列名的创建:
        姓名  成绩
    0  小红  85
    1  小花  93
    2  小兰  99
    字典方式创建:
           apart  months  profits  year
    one     101       8    681.5  2007
    two     102       8    125.0  2010
    three   103       8     15.5  2012
    four    104       8    160.0  2008

    三、DataFrame查询

    DataFrame可以直接通过列索引名,查看或修改整列值

    如果想通过行索引名查询的话,需要借助ix('行索引名')

    import pandas as pd
    data = {
        'apart': ['101', '102', '103', '104'],
        'profits': [681.5, 125, 15.5, 160],
        'year': [2007, 2010, 2012, 2008],
        'months': 8
    }
    c = pd.DataFrame(data, index=['one', 'two', 'three', 'four'])
    print("列索引:
    ", c['year'])
    print("行索引:
    ", c.ix['three'])
    >>>
    列索引:
    one      2007
    two      2010
    three    2012
    four     2008
    Name: year, dtype: int64
    行索引:
    apart       103
    months        8
    profits    15.5
    year       2012
    Name: three, dtype: object

    四、数据过滤

    • dropna:丢弃(删除)有NaN的行, 可以通过阈值(how参数)的调节对缺失值的容忍度
    • fillna:用指定值或者插值的方式填充缺失数据,比如: ffill或者bfill
    • isnull:返回一个含有布尔值的对象,这些布尔值表示那些值是缺失值NA
    • notnull:返回布尔值对象,非空位为True
    ############这里演示一个特殊的填充方式############
    import pandas as pd
    import numpy as np
    a = pd.DataFrame(np.random.rand(7, 3))
    a.ix[:4, 1] = np.NaN
    a.ix[:2, 2] = np.NaN
    a=a.fillna({1: 0.5, 2: -1, 3: 2})#按照列索引进行填充
    print(a)
    >>>
              0         1         2
    0  0.467758  0.500000 -1.000000
    1  0.436149  0.500000 -1.000000
    2  0.917665  0.500000 -1.000000
    3  0.473428  0.500000  0.113061
    4  0.703225  0.500000  0.090117
    5  0.926495  0.962959  0.005762
    6  0.414894  0.753270  0.243547
    
    进程已结束,退出代码0
    

    五、DataFrame运算

    DataFrame也保留了大量Numpy的运算机制,它们在DataFrame中均可以使用。

    DataFrame中默认是按照列索引进行计算的,如果要按照行索引计算,需要在方法后面设置维度参数axis=1

    import pandas as pd
    a = pd.DataFrame(
        [
            [98.5, 89.5, 88.5],
            [98.5, 85.5, 80.0],
            [70.0, 85.0, 60.0],
            [80.0, 85.0, 82.0]
        ], columns=['语文', '数学', '英语']
    )
    print("原数组:
    ", a)
    print("统计结果:
    ", a.describe())
    >>>
    统计结果:
                   语文         数学         英语
    count   4.000000   4.000000   4.000000
    mean   86.750000  86.250000  77.625000
    std    14.168627   2.179449  12.297527
    min    70.000000  85.000000  60.000000
    25%    77.500000  85.000000  75.000000
    50%    89.250000  85.250000  81.000000
    75%    98.500000  86.500000  83.625000
    max    98.500000  89.500000  88.500000
    
  • 相关阅读:
    win10应用 UWP 使用MD5算法
    win10应用 UWP 使用MD5算法
    用git上传代码到新浪云
    用git上传代码到新浪云
    redis三节点sentinel部署
    [HNOI/AHOI2018]转盘
    用git上传代码到新浪云
    Windows-universal-samples-master示例 XamlCommanding
    Windows-universal-samples-master示例 XamlCommanding
    Windows-universal-samples-master示例 XamlCommanding
  • 原文地址:https://www.cnblogs.com/hezhefly/p/8284260.html
Copyright © 2011-2022 走看看