• 【Pandas总结】第一节 Pamdas 简介与Series,DataFrame的创建


    一、Pandas 简介

    Pandas是使用Python语言开发的用于数据处理和数据分析的第三方库。它擅长处理数字型数据和时间序列数据,当然文本型的数据也能轻松处理。
    Pandas 可以处理的数据格式非常多,常见的数据文件格式都可以快速导入,比如 CSV、JSON、SQL、Microsoft Excel 导入数据。
    由于Pandas对数据的强大处理能力,被广泛的应用于金融,数据处理等方面,是大家学习数据处理时,不可避免的存在;如果想要学好数据处理,就一起来学习Pandas吧!!!
    总结:Pandas 在数据处理方面很强大!封装了我们常用的功能,直接调用相关的函数即可,省去了我们很多工作,让我们一起好好学习Pandas吧!

    二、Pandas 数据结构

    Pandas 的主要数据结构有两种,分别是: Series (一维数据)与 DataFrame(二维数据);所以后面的内容也是围绕着这两部门展开的;

    2.1 Series (一维数据)

    似于一维数组的对象,它由一组数据(各种Numpy数据类型)以及一组与之相关的数据标签(即索引)组成。即:Series 与Numpy 数组基本是一样的,只不过多了数据标签(索引);

    Series 格式: pandas.Series( data, index, dtype, name, copy)

    参数说明:

    参数解释说明
    data一组数据(ndarray 类型) ,即Numpy数据;
    index数据索引标签,如果不指定,默认从 0 开始。
    dtype数据类型,默认会自己判断。
    name设置名称。(不常用)
    copy拷贝数据,默认为 False。(不常用)

    Series 举例:

    import pandas as pd
    
    a = ['x','y','z']
    b = [1,2,3]
    mynum1 = pd.Series(b)
    print(mynum1)
    # 0    1
    # 1    2
    # 2    3
    
    mynum2 = pd.Series(b,index=a)
    print(mynum2)
    # x    1
    # y    2
    # z    3
    # dtype: int64
    
    mynum3 = pd.Series(b,index=a,dtype=float)
    print(mynum3)
    # x    1.0
    # y    2.0
    # z    3.0
    # dtype: float64
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    注意事项:

    1. 一般情况下,当我们导入pandas的时候,都会将Pandas重命名为pd(因为大家都这么做;)
    2. 当不指定index时,会默认index是一个从0开始的整数数组,即:0,1,2…;例如:上例中的mynum1;
    3. 当不指定dtype时,pandas会自己判断一个最合适的数据类型;例如:上例中的mynum2;
    4. 也可以自己指定index与dtype, 这样pandas会按照我们指定的数据生成;例如:上例中的mynum3;

    2.2 DataFrame(二维数据)

    类似于二维数组的对象,是一个表格型的数据结构,它含有一组有序的列,每列可以是不同的值类型(数值、字符串、布尔型值)。DataFrame 既有行索引也有列索引;可以当作是许多个共用同一个索引的Series组成的数据结构;DataFrame 图解如下:

    在这里插入图片描述
    上图所表示的就是一个由n 个Series组成的DataFrame; 所有的Series 共享同一个Index ;

    DataFrame 格式:pandas.DataFrame( data, index, columns, dtype, copy)

    参数说明:

    参数解释说明
    data一组数据(ndarray 类型) ,即Numpy数据;
    index数据索引标签,也可称为行标签
    columns数据列标签,如果不指定,默认从 0 开始。也可称为列标签。
    dtype数据类型,默认会自己判断。
    copy拷贝数据,默认为 False。(不常用)

    三、Series 的创建

    3.1 由数创建

    不指定Index的时候,用单个数值只能创建一个元素的Series;

    mySer = pd.Series(2)
    print(mySer)  
    # 0    2
    # dtype: int64
    
    • 1
    • 2
    • 3
    • 4

    通过指定Index 可以生成多个相同元素的Series,举例如下:

    mySer = pd.Series(2,index=range(3))
    print(mySer)
    # 0    2
    # 1    2
    # 2    2
    # dtype: int64
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.2 由列表创建

    当不指定Index的时候,会默认生成RangeIndex(start=0, stop=n-1, step=1)的Index; 举例如下:

    lis = [3,4,5]
    mySer = pd.Series(lis)
    print(mySer)
    # 0    3
    # 1    4
    # 2    5
    # dtype: int64
    print(mySer.index)
    # RangeIndex(start=0, stop=3, step=1)
    print(mySer.values)
    # [3 4 5]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    也可以指定Index, 举例如下:

    lis = [3,4,5]
    ind = ["马里奥","路易吉","林克"]
    mySer = pd.Series(lis,index=ind)
    print(mySer)
    # 马里奥    3
    # 路易吉    4
    # 林克     5
    # dtype: int64
    print(mySer.index)
    # Index(['马里奥', '路易吉', '林克'], dtype='object')
    print(mySer.values)
    # [3 4 5]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3.3 由字典创建

    dic = {'马里奥':3,'路易吉':4,'林克':5}
    mySer = pd.Series(dic)
    print(mySer)   
    # 马里奥    3
    # 路易吉    4
    # 林克     5
    # dtype: int64
    print(mySer.index)  
    # Index(['马里奥', '路易吉', '林克'], dtype='object')
    print(mySer.values)
    # [3 4 5]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3.4 由Numpy数组创建

    array = np.array([0, 1, 2, 3])
    mySer = pd.Series(array)
    print(mySer)   
    # 0    0
    # 1    1
    # 2    2
    # 3    3
    # dtype: int32
    print(mySer.index)
    # RangeIndex(start=0, stop=4, step=1)
    print(mySer.values)
    # [0 1 2 3]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    四、DataFreme 的创建

    由字典创建

    dict1 = {'马里奥':[2,3,4],'路易吉':[5,6,7],'林克':[8,9,0]}
    df = pd.DataFrame(dict1,index=["英雄%s"%i for i in range(1,4)])
    print(df)
    #      马里奥  路易吉  林克
    # 英雄1    2     5     8      
    # 英雄2    3     6     9      
    # 英雄3    4     7     0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    由Series创建

    ser1 = pd.Series(np.arange(0,5,1))   
    ser2 = pd.Series(np.arange(5,10,1))  
    df = pd.DataFrame({'A':ser1,'B':ser2})
    print(df)
    #    A  B
    # 0  0  5
    # 1  1  6
    # 2  2  7
    # 3  3  8
    # 4  4  9
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    由二维数组创建

    arr = np.arange(9).reshape(3,3)
    df = pd.DataFrame(arr,columns=['A','B','C'],index=['IN1','IN2','IN3'])
    print(df)
    #      A  B  C
    # IN1  0  1  2
    # IN2  3  4  5
    # IN3  6  7  8
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    由列表创建

    lis1=[1,2,3]
    lis2=[2,3,4]
    df = pd.DataFrame({'A':lis1,'B':lis2},index=['IN1','IN2','IN3'])
    print(df)
    #      A  B
    # IN1  1  2
    # IN2  2  3
    # IN3  3  4
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    MySQL性能优化 - 别再只会说加索引了
    JMeter - 如何测试REST API / 微服务
    EDA 虚拟机 Synopsys Sentaurus TCAD 2017.09 下载
    微服务架构详解
    20个SQL查询优化技巧
    72编辑距离
    数字孪生城市,智慧城市可视化技术解决方案案例
    Unity/C# 把时间转换成时间戳的方法
    软件设计模式系列之十四——代理模式
    刷完这个笔记,17K不能再少了....
  • 原文地址:https://blog.csdn.net/weixin_47139649/article/details/126635732