zoukankan      html  css  js  c++  java
  • pands基础--数据结构:Series

    从本文开始介绍pandas的相关知识。

    pandas含有是数据分析工作变得更快更简单的高级数据结构和操作工具,是基于numpy构建的。

    本章节的代码引入pandas约定为:import pandas as pd,另外import numpy as np也会用到。

    pandas数据结构介绍:主要有两种:Series和DataFrame。本文对Series进行简单介绍。

    (1)简介

    Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据结构)以及一组与之相关的数据标签(即索引)组成。

    1 >>> obj = pd.Series([4, 7, -5, 3])  
    2 >>> obj
    3 0    4
    4 1    7
    5 2   -5
    6 3    3
    7 dtype: int64

    (2)表现形式

    Series的字符串表现形式为:索引在左边,值在右边。若没有为数据指定索引,则会自动创建一个0到N-1(N为数据的长度)的整数型索引。可通过Series的values和index属性获取其数组表示形式和索引对象。

     1 >>> obj
     2 0    4
     3 1    7
     4 2   -5
     5 3    3
     6 dtype: int64
     7 >>>
     8 >>> obj.index
     9 RangeIndex(start=0, stop=4, step=1)
    10 >>> obj.values
    11 array([ 4,  7, -5,  3], dtype=int64)
    12 >>>

    如果希望所创建的Series带有一个可以对各个数据点进行标记的索引:

    1 >>> obj2 = pd.Series([4, 5, 7, -3], index=['a', 'b', 'c', 'd'])
    2 >>> obj2
    3 a    4
    4 b    5
    5 c    7
    6 d   -3
    7 dtype: int64
    8 >>> obj2.index
    9 Index(['a', 'b', 'c', 'd'], dtype='object')

    也可以通过字典来创建Series。如果只传入一个字典,则结果Series中的索引就是原字典的键(有序排列)。

     1 >>> sdata = {'a':100, 'b':200, 'c':300} 
     2 >>> obj3 = pd.Series(sdata) 
     3 >>> obj3
     4 a    100
     5 b    200
     6 c    300
     7 dtype: int64
     8 >>> index = ['a', 'd', 'c']    
     9 >>> obj4 = pd.Series(sdata, index=index)
    10 >>> obj4
    11 a    100.0
    12 d      NaN
    13 c    300.0
    14 dtype: float64
    15 >>>

    上面的例子中,索引为’d’的值为NaN,即“非数字”(not a number)。在pandas中,它用于表示确实或者NA值,使用缺失或NA表示缺失数据,另外isnull和notnull函数可用于检测缺失数据。

     1 >>> obj4
     2 a    100.0
     3 d      NaN
     4 c    300.0
     5 dtype: float64
     6 >>>
     7 >>> pd.isnull(obj4)
     8 a    False
     9 d     True
    10 c    False
    11 dtype: bool
    12 >>> pd.notnull(obj4)  
    13 a     True
    14 d    False
    15 c     True
    16 dtype: bool

    (3)索引方式

    与普通NumPy数组相比,可以通过索引的方式选取Series中的单个或一组值。

    1 >>> obj2['a'] 
    2 4
    3 >>> obj2[['a', 'b', 'c']]
    4 a    4
    5 b    5
    6 c    7
    7 dtype: int64
    8 >>>

    还可以将Series看作一个定长的有序字典,因为它是索引值到数据值的一个映射。

    1 >>> 'b' in obj2 
    2 True
    3 >>> 'e' in obj2 
    4 False

    series的索引可以通过赋值的方式就地修改。

     1 >>> obj
     2 0    4
     3 1    7
     4 2   -5
     5 3    3
     6 dtype: int64
     7 >>> obj.index = ['a', 'b', 'c', 'd']
     8 >>> obj
     9 a    4
    10 b    7
    11 c   -5
    12 d    3
    13 dtype: int64
    14 >>>

    (4)基本运算

    NumPy数组运算(如根据布尔型数组进行过滤、标量乘法、应用数学函数等)都会保留索引和值之间的链接。

     1 >>> obj2
     2 a    4
     3 b    5
     4 c    7
     5 d   -3
     6 dtype: int64
     7 >>> obj2[obj2 > 0] 
     8 a    4
     9 b    5
    10 c    7
    11 dtype: int64
    12 >>> obj2 * 2      
    13 a     8
    14 b    10
    15 c    14
    16 d    -6
    17 dtype: int64
    18 >>> np.exp(obj2)       
    19 a      54.598150
    20 b     148.413159
    21 c    1096.633158
    22 d       0.049787
    23 dtype: float64
    24 >>>

    (5)其他

    对应用而言,Series最重要的一个功能是:它在算术运算中会自动对齐不同索引的数据。这个功能在后面进行讲解。

    Series对象本身及其索引都有一个name属性,该属性跟pandas其他的关键功能关系非常密切。

     1 >>> obj4 
     2 a    100.0
     3 d      NaN
     4 c    300.0
     5 dtype: float64
     6 >>> obj4.name = 'n1' 
     7 >>> obj4.index.name = 'pharse' 
     8 >>> obj4
     9 pharse
    10 a    100.0
    11 d      NaN
    12 c    300.0
    13 Name: n1, dtype: float64 
  • 相关阅读:
    LeetCode Path Sum II
    LeetCode Longest Palindromic Substring
    LeetCode Populating Next Right Pointers in Each Node II
    LeetCode Best Time to Buy and Sell Stock III
    LeetCode Binary Tree Maximum Path Sum
    LeetCode Find Peak Element
    LeetCode Maximum Product Subarray
    LeetCode Intersection of Two Linked Lists
    一天一个设计模式(1)——工厂模式
    PHP迭代器 Iterator
  • 原文地址:https://www.cnblogs.com/mrlayfolk/p/12252035.html
Copyright © 2011-2022 走看看