zoukankan      html  css  js  c++  java
  • pandas(Series一)

    pandas:数据分析

    • pandas是一个强大的的python数据分析的工具包
    • pandas是基于NumPy构建的
    • pandas的主要功能:
      • 具备对其功能对数据结构DataFrame, Series
      • 集成时间序列功能
      • 提供丰富的数学运算和操作
      • 灵活处理缺失数据

    安装方法:pip install pandas

    引用方法:import pandas as pd

    pandas:Series

    Series是一种类似于一维数组的对象,有一组数据和一组与之相关的数据标签(索引)组成

    Series比较像列表(数组)和字典的结合体

    创建方式

    In [62]: pd.Series([1,2,3,-6,7])
    Out[62]: 
    0    1
    1    2
    2    3
    3   -6
    4    7
    dtype: int64
    
    
    In [67]: pd.Series([1,2,3,-6,7],index=['a', 'b', 'c', 'd', 'e'])
    Out[67]: 
    a    1
    b    2
    c    3
    d   -6
    e    7
    dtype: int64
    
    
    In [69]: pd.Series({'a':11, 'b':22})
    Out[69]: 
    a    11
    b    22
    dtype: int64
    
    In [70]: pd.Series(0, index=['a', 'b', 'd'])
    Out[70]: 
    a    0
    b    0
    d    0
    dtype: int64

    获取值数组和索引数组:values属性和index属性

    In [71]: a = pd.Series([11,22,33,44,55], index=['a', 'b', 'c','d', 'e'])
    
    In [72]: a
    Out[72]: 
    a    11
    b    22
    c    33
    d    44
    e    55
    dtype: int64
    
    In [73]: a.values
    Out[73]: array([11, 22, 33, 44, 55])
    
    In [74]: a.index
    Out[74]: Index([u'a', u'b', u'c', u'd', u'e'], dtype='object'

    pandas:Series特性

    Series支持NumPy模块的特性
    从ndarray创建Series: Series(arr)
    In [75]: a = pd.Series(np.array([11,22,33,44]))
    
    In [76]: a
    Out[76]: 
    0    11
    1    22
    2    33
    3    44
    dtype: int64
    
    与标量运算:sr*2
    In [75]: a = pd.Series(np.array([11,22,33,44]))
    
    In [76]: a
    Out[76]: 
    0    11
    1    22
    2    33
    3    44
    dtype: int64
    
    In [77]: a+10
    Out[77]: 
    0    21
    1    32
    2    43
    3    54
    dtype: int64
    
    In [79]: a*2
    Out[79]: 
    0    22
    1    44
    2    66
    3    88
    dtype: int64
    
    两个Series运算:sr1+sr2
    In [75]: a = pd.Series(np.array([11,22,33,44]))
    In [80]: b = pd.Series([1,2,3,4])
    
    In [81]: a+b
    Out[81]: 
    0    12
    1    24
    2    36
    3    48
    dtype: int64
    
    索引:sr[0], sr[[0,2,4]]花式索引
    In [75]: a = pd.Series(np.array([11,22,33,44]))
    In [82]: a[0]
    Out[82]: 11
    
    In [85]: a[[0,2,3]]
    Out[85]: 
    0    11
    2    33
    3    44
    dtype: int64
    
    切片: sr[0:2]
    In [75]: a = pd.Series(np.array([11,22,33,44]))
    In [86]: a[0:3]
    Out[86]: 
    0    11
    1    22
    2    33
    dtype: int64
    
    布尔值过滤:sr[sr>0]
    In [75]: a = pd.Series(np.array([11,22,33,44]))
    In [87]: a[a>30]
    Out[87]: 
    2    33
    3    44
    dtype: int64
    
    通用函数:np.abs(sr)
    In [91]: a = pd.Series(-2)
    
    In [92]: a
    Out[92]: 
    0   -2
    dtype: int64
    
    In [93]: np.abs(a)
    Out[93]: 
    0    2
    dtype: int64

    Series支持字典的特性

    从字典创建Series: Series(dic)
    In [94]: a = pd.Series({'a':10, 'b':20, 'c':30})
    
    In [95]: a
    Out[95]: 
    a    10
    b    20
    c    30
    dtype: int64
    
    in运算:'a' in sr
    In [96]: 'c' in a
    Out[96]: True
    
    键索引:sr['a'], sr[['a', 'b', 'c']]
    In [97]: a['b']
    Out[97]: 20
    
    In [98]: a[['a', 'c']]
    Out[98]: 
    a    10
    c    30
    dtype: int64

    pandas:整数索引

    整数索引的pandas对象错误示例
    sr = np.Series(np.arange(4))
    sr[-1]
    
    如果索引是整数类型,则根据整数进行数据操作时总是面向标签的
    loc 以标签解释
    iloc 以下标解释
    #loc以标签解释
    In [123]: a = pd.Series([11,22,33,44], index=[11,22,33,44])
    In [124]: a
    Out[124]: 
    11    11
    22    22
    33    33
    44    44
    dtype: int64
    
    In [125]: a.loc[11]
    Out[125]: 11
    In [126]: a.loc[11:33]
    Out[126]: 
    11    11
    22    22
    33    33
    dtype: int64
    
    #iloc以下标解释
    In [131]: a.iloc[-1]
    Out[131]: 44
    
    In [133]: a.iloc[0:3]
    Out[133]: 
    11    11
    22    22
    33    33
    dtype: int64
  • 相关阅读:
    Sublime Text 3 免费注册方法(福利)
    点击查看大图滑动预览(h5,pc通用)
    react中简单倒计时跳转
    mui.ajax中文乱码
    pycharm的一些快捷键
    Flex
    justify-content
    mysql分组,行转列
    前端资源教程
    mui初级入门教程(七)— 基于native.js的文件系统管理功能实现
  • 原文地址:https://www.cnblogs.com/YingLai/p/9286846.html
Copyright © 2011-2022 走看看