Python數(shù)據(jù)分析與展示:Series類型簡單操作-8
發(fā)布時間:2021-11-23 點擊數(shù):733
import pandas as pd
Pandas基于NumPy實現(xiàn),常與NumPy和Matplotlib一同使用
兩個數(shù)據(jù)類型:Series, DataFrame
基于上述數(shù)據(jù)類型的各類操作
- 基本操作
- 運算操作
- 特征類操作
- 關(guān)聯(lián)類操作
|
庫 |
NumPy |
Pandas |
|
數(shù)據(jù)類型 |
基礎(chǔ) |
擴展 |
|
關(guān)注數(shù)據(jù) |
結(jié)構(gòu)表達 |
應(yīng)用表達 |
|
維度關(guān)系 |
數(shù)據(jù)間關(guān)系 |
數(shù)據(jù)與索引間關(guān)系 |
Series類型
Series類型由一組數(shù)據(jù)及與之相關(guān)的數(shù)據(jù)索引組成
- 自動索引
- 自定義索引
Series是一維帶“標簽”數(shù)組
結(jié)構(gòu):data_a index_0
Series基本操作類似ndarray和字典,根據(jù)索引對齊
Series類型創(chuàng)建:
- Python列表,index與列表元素個數(shù)一致
- 標量值,index表達Series類型的尺寸
- Python字典,鍵值對中的“鍵”是索引,index從字典中進行選擇操作
- ndarray,索引和數(shù)據(jù)都可以通過ndarray類型創(chuàng)建
- 其他函數(shù),range()函數(shù)等
Series類型基本操作
- Series類型包括index和values兩部分
- .index 獲得索引
- .values 獲得數(shù)據(jù)
-
Series類型的操作類似ndarray類型
- 索引方法相同,采用[]
- NumPy中運算和操作可用于Series類型
- 可以通過自定義索引的列表進行切片
- 可以通過自動索引進行切片,如果存在自定義索引,則一同被切片
- Series類型的操作類似Python字典類型:
- 通過自定義索引訪問
- 保留字in操作
- 使用.get()方法
Series類型對齊操作
Series+ Series
Series類型在運算中會自動對齊不同索引的數(shù)據(jù)
Series類型name屬性
Series對象和索引都可以有一個名字,存儲在屬性.name中
Series類型的修改
對獲取的值進行賦值
代碼示例
# -*- coding: utf-8 -*- # @File : series_demo.py # @Date : 2018-05-19 import pandas as pd # 創(chuàng)建Series對象 d = pd.Series(range(5)) print(d) """ 0 0 1 1 2 2 3 3 4 4 dtype: int64 """ # 計算前N項和 print(d.cumsum()) """ 0 0 1 1 2 3 3 6 4 10 dtype: int64 """ # 自動索引 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 """ # 從標量值創(chuàng)建, 不能省略index s = pd.Series(20, index=["a", "b", "c"]) print(s) """ a 20 b 20 c 20 dtype: int64 """ # 從字典類型創(chuàng)建 s = pd.Series({"a": 1, "b": 2, "c": 3}) print(s) """ a 1 b 2 c 3 dtype: int64 """ # index從字典中進行選擇操作 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類型創(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] # 自動索引和自定義索引并存 但不能混 print(s[0]) # 1 print(s["a"]) # 1 # 切片操作 print(s[["a", "b"]]) """ a 1 b 2 dtype: int64 """ # 類似ndarray類型 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 """ # 類似Python字典類型 print("b" in s) # True print(s.get("g", 100)) # 100 # Series類型對齊操作 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類型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 """
