PANDAS核心数据结构

    SERIES

    1. #series定义
    2. #series可以理解为一个一维的数组,只是index名称可以自己改动。
    3. #类似于的定长的有序字典,有index和value。index赋值必须是list类型
    1. #创建函数
    2. #pandas.Series(data=None,index=None,dtype=None,name=None,copy=False,fastpath=False)
    3. #参数名称 说明
    4. #data 数据源
    5. #index 索引,赋值必须为列表
    6. #dtype 元素数据类型
    7. #name Series的名称
    1. import numpy as np
    2. import pandas as pd
    1. #创建一个空系列
    2. s = pd.Series()
    3. s
    1. Series([], dtype: float64)
    1. #从ndarray创建一个Series
    2. data = np.array(['张三','李四','王五','赵柳'])
    3. s1 = pd.Series(data,index = ['100','101','102','103'],
    4. name = '姓名')
    5. s1
    1. 100 张三
    2. 101 李四
    3. 102 王五
    4. 103 赵柳
    5. Name: 姓名, dtype: object
    1. #从字典创建Series
    2. data = {'S100': '张三', 'S101': '李四', 'S102': '王五', 'S103': '赵六', 'S104': '杨七'}
    3. s2 = pd.Series(data)
    4. s2
    1. S100 张三
    2. S101 李四
    3. S102 王五
    4. S103 赵六
    5. S104 杨七
    6. dtype: object
    1. #从标量创建一个Series
    2. s3 = pd.Series(5,index = [0,1,2,3,4])
    3. s3
    1. 0 5
    2. 1 5
    3. 2 5
    4. 3 5
    5. 4 5
    6. dtype: int64

    Series(系列)-属性

    1. s1.index
    1. Index(['100', '101', '102', '103'], dtype='object')
    1. s1.values
    1. array(['张三', '李四', '王五', '赵柳'], dtype=object)
    1. s1.array
    1. ---------------------------------------------------------------------------
    2. AttributeError Traceback (most recent call last)
    3. <ipython-input-27-3cde8a24ae24> in <module>()
    4. ----> 1 s1.array
    5. E:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
    6. 4374 if self._info_axis._can_hold_identifiers_and_holds_name(name):
    7. 4375 return self[name]
    8. -> 4376 return object.__getattribute__(self, name)
    9. 4377
    10. 4378 def __setattr__(self, name, value):
    11. AttributeError: 'Series' object has no attribute 'array'
    1. # Series.index 系列的索引(轴标签)
    2. # Series.array支持该系列或索引的数据的ExtensionArray
    3. # Series.values Series的值。根据dtype将Series返回为ndarray或类似ndarray
    4. # Series.dtype 返回基础数据的dtype对象
    5. # Series.shape 返回基础数据形状的元组
    6. # Series.nbytes 返回基础数据中的字节数
    7. # Series.ndim 返回基础数据的维数(轴数)
    8. # Series.size 返回基础数据中的元素数
    9. # Series.T 返回转置
    10. # Series.dtypes 返回基础数据的dtype对象
    11. # Series.memory_usage([index,deep])返回该系列的内存使用情况
    12. # Series.hasnans 如果有nans,就返回True
    13. # Series.empty 指示DataFrame是否为空,如果为空则返回True
    14. # Series.name 返回系列的名称