亚洲一区精品自拍_2021年国内精品久久_男同十八禁gv在线观看_免费观看a级性爱黄片

當(dāng)前位置:文章中心>技術(shù)教程
公告通知 新聞快遞 技術(shù)教程 產(chǎn)品展示

Python數(shù)據(jù)分析與展示:Series類(lèi)型簡(jiǎn)單操作-8

發(fā)布時(shí)間:2021-11-23 點(diǎn)擊數(shù):735
import pandas as pd 

Pandas基于NumPy實(shí)現(xiàn),常與NumPy和Matplotlib一同使用

兩個(gè)數(shù)據(jù)類(lèi)型:Series, DataFrame

基于上述數(shù)據(jù)類(lèi)型的各類(lèi)操作

  • 基本操作
  • 運(yùn)算操作
  • 特征類(lèi)操作
  • 關(guān)聯(lián)類(lèi)操作


庫(kù)

NumPy

Pandas

數(shù)據(jù)類(lèi)型

基礎(chǔ)

擴(kuò)展

關(guān)注數(shù)據(jù)

結(jié)構(gòu)表達(dá)

應(yīng)用表達(dá)

維度關(guān)系

數(shù)據(jù)間關(guān)系

數(shù)據(jù)與索引間關(guān)系


Series類(lèi)型

Series類(lèi)型由一組數(shù)據(jù)及與之相關(guān)的數(shù)據(jù)索引組成

  • 自動(dòng)索引
  • 自定義索引

Series是一維帶“標(biāo)簽”數(shù)組

結(jié)構(gòu):data_a index_0

Series基本操作類(lèi)似ndarray和字典,根據(jù)索引對(duì)齊

Series類(lèi)型創(chuàng)建:

  • Python列表,index與列表元素個(gè)數(shù)一致
  • 標(biāo)量值,index表達(dá)Series類(lèi)型的尺寸
  • Python字典,鍵值對(duì)中的“鍵”是索引,index從字典中進(jìn)行選擇操作
  • ndarray,索引和數(shù)據(jù)都可以通過(guò)ndarray類(lèi)型創(chuàng)建
  • 其他函數(shù),range()函數(shù)等

Series類(lèi)型基本操作

  1. Series類(lèi)型包括index和values兩部分
    • .index 獲得索引
    • .values 獲得數(shù)據(jù)
  1. Series類(lèi)型的操作類(lèi)似ndarray類(lèi)型
  • 索引方法相同,采用[]
  • NumPy中運(yùn)算和操作可用于Series類(lèi)型
  • 可以通過(guò)自定義索引的列表進(jìn)行切片
  • 可以通過(guò)自動(dòng)索引進(jìn)行切片,如果存在自定義索引,則一同被切片
  1. Series類(lèi)型的操作類(lèi)似Python字典類(lèi)型:
    • 通過(guò)自定義索引訪問(wèn)
    • 保留字in操作
    • 使用.get()方法

Series類(lèi)型對(duì)齊操作

Series+ Series

Series類(lèi)型在運(yùn)算中會(huì)自動(dòng)對(duì)齊不同索引的數(shù)據(jù)

Series類(lèi)型name屬性

Series對(duì)象和索引都可以有一個(gè)名字,存儲(chǔ)在屬性.name中

Series類(lèi)型的修改

對(duì)獲取的值進(jìn)行賦值


代碼示例

# -*- coding: utf-8 -*-  # @File    : series_demo.py # @Date    : 2018-05-19  import pandas as pd  # 創(chuàng)建Series對(duì)象 d = pd.Series(range(5))  print(d) """ 0    0 1    1 2    2 3    3 4    4 dtype: int64 """  # 計(jì)算前N項(xiàng)和 print(d.cumsum()) """ 0     0 1     1 2     3 3     6 4    10 dtype: int64 """  # 自動(dòng)索引 d = pd.Series([1, 2, 3, 4, 5]) print(d) """ 0    1 1    2 2    3 3    4 4    5 dtype: int64 """  # 自定義索引 d = pd.Series([1, 2, 3, 4, 5], index=["a", "b", "c", "d", "e"]) print(d) """ a    1 b    2 c    3 d    4 e    5 dtype: int64 """  # 從標(biāo)量值創(chuàng)建, 不能省略index s = pd.Series(20, index=["a", "b", "c"]) print(s) """ a    20 b    20 c    20 dtype: int64 """  # 從字典類(lèi)型創(chuàng)建 s = pd.Series({"a": 1, "b": 2, "c": 3}) print(s) """ a    1 b    2 c    3 dtype: int64 """  # index從字典中進(jìn)行選擇操作 s = pd.Series({"a": 1, "b": 2, "c": 3}, index=["c", "a", "b", "d"]) print(s) """ c    3.0 a    1.0 b    2.0 d    NaN dtype: float64 """  # 從ndarray類(lèi)型創(chuàng)建 import numpy as np s = pd.Series(np.arange(5)) print(s) """ 0    0 1    1 2    2 3    3 4    4 dtype: int32 """  # 指定索引 s = pd.Series(np.arange(5), index=np.arange(9, 4, -1)) print(s) """ 9    0 8    1 7    2 6    3 5    4 dtype: int32 """  # Series基本操作 s = pd.Series([1, 2, 3, 4, 5], index=["a", "b", "c", "d", "e"])  # 獲得索引 print(s.index) # Index(['a', 'b', 'c', 'd', 'e'], dtype='object')  # 獲得值 print(s.values) # [1 2 3 4 5]  # 自動(dòng)索引和自定義索引并存 但不能混 print(s[0]) # 1  print(s["a"]) # 1  # 切片操作 print(s[["a", "b"]]) """ a    1 b    2 dtype: int64 """  # 類(lèi)似ndarray類(lèi)型 print(s[:3]) """ a    1 b    2 c    3 dtype: int64 """  print(s[s>s.median()]) """ d    4 e    5 dtype: int64 """  print(np.exp(s)) """ a      2.718282 b      7.389056 c     20.085537 d     54.598150 e    148.413159 dtype: float64 """  # 類(lèi)似Python字典類(lèi)型 print("b" in s) # True  print(s.get("g", 100)) # 100  # Series類(lèi)型對(duì)齊操作 a = pd.Series([1, 2, 3], index=["a", "b", "c"]) b = pd.Series([5, 6, 7, 8], index=["a", "b", "d", "e"])  print(a+b) """ a    6.0 b    8.0 c    NaN d    NaN e    NaN dtype: float64 """  # Series類(lèi)型name屬性 s = pd.Series([1, 2, 3, 4, 5], index=["a", "b", "c", "d", "e"]) s.name="Series" s.index.name = "索引" print(s) """ 索引 a    1 b    2 c    3 d    4 e    5 Name: Series, dtype: int64 """  # Series修改 s = pd.Series([1, 2, 3, 4, 5], index=["a", "b", "c", "d", "e"]) s[0] = 666 print(s) """ 0    666 1      2 2      3 3      4 4      5 dtype: int64 """  s["a", "b"] = 20 print(s) """ a    20 b    20 c     3 d     4 e     5 dtype: int64 """  # Series刪除元素 s = pd.Series([1, 2, 3, 4, 5, 6], index=["a", "b", "c", "d", "e", "f"]) print(s) """ a    1 b    2 c    3 d    4 e    5 f    6 dtype: int64 """ s1 = s.drop(["a", "b"]) print(s1) """ c    3 d    4 e    5 f    6 dtype: int64 """