1. Pandas簡介
Pandas是一個開源的,BSD許可的庫,爲Python編程語言提供高性能,易於使用的數據結構和數據分析工具。
2. Series用法
Pandas提供了兩種最重要的數據結構類型,Series(一維)和DataFrame(二維),我們今天先來介紹Series。
Series 是一組帶索引數組,即由一組數據和與之相關的索引組成的一維數據對象。與list以及numpy中array相似,但list中元素可以爲不同數據類型,而另外兩者只能爲相同數據類型。
2.1 如何創建Series
- 通過列表
list
創建Series
- 通過指定
index
關鍵字的方式創建帶有自定義索引的Series
- 通過array創建
Series
- 通過字典
dict
創建Series
import pandas as pd
import numpy as np
s1 = pd.Series([10, 40, 5, 90, 35, 40])
print(s1)
# 0 10
# 1 40
# 2 5
# 3 90
# 4 35
# 5 40
# dtype: int64
s2 = pd.Series([100, 79, 65, 77], index=["chinese", "english", "history", "maths"], name='score')
# chinese 100
# english 79
# history 65
# maths 77
# Name: score, dtype: int64
s3 = pd.Series(np.linspace(start=0, stop=5, num=5))
print(s3)
# 0 0.00
# 1 1.25
# 2 2.50
# 3 3.75
# 4 5.00
# dtype: float64
s4 = pd.Series(np.arange(10, 15))
print(s4)
# 0 10
# 1 11
# 2 12
# 3 13
# 4 14
# dtype: int32
d1 = pd.Series({"name": "張三", "Gender": "男", "age": 20, "height": 180, "weight": 66})
# name 張三
# Gender 男
# age 20
# height 180
# weight 66
# dtype: object
2.2 如何獲取Series的屬性
index
索引values
值name
名字dtype
類型
print(s1.index)
# RangeIndex(start=0, stop=6, step=1)
print(s1.values)
# [10 40 5 90 35 40]
print(s1.name)
# None
print(s1.dtype)
# int64
print(s2.index)
# Index(['chinese', 'english', 'history', 'maths'], dtype='object')
print(s2.values)
# [100 79 65 77]
2.3 如何獲取數據
- 通過位置獲取數據
- 通過索引獲取數據
print(s1[0])
# 10
# 獲取值
print(s1[0:2])
# 0 10
# 1 40
# dtype: int64
print(s1[[0, 1, 2]])
# 0 10
# 1 40
# 2 5
# dtype: int64
s2["chinese"]
# 100
s2[["english", "history"]]
# english 79
# history 65
# dtype: int64
print(s2[s2.values > 70])
# chinese 100
# english 79
# maths 77
# Name: score, dtype: int64
print(s2[s2.index != 'chinese'])
# english 79
# history 65
# maths 77
# Name: score, dtype: int64
d1["name"]
# 張三
d1[["name", "height", "weight"]]
# name 張三
# height 180
# weight 66
# dtype: object
2.4 基本運算
查看描述性統計數據
print(s1.values)
# [10 40 5 90 35 40]
print(s1.describe())
# count 6.000000
# mean 36.666667
# std 30.276504
# min 5.000000
# 25% 16.250000
# 50% 37.500000
# 75% 40.000000
# max 90.000000
# dtype: float64
print(s1.mean()) #均值
# 36.666666666666664
print(s1.median()) #中位數
# 37.5
print(s1.sum()) #求和
# 220
print(s1.std()) #標準差
# 30.276503540974915
print(s1.mode()) #衆數
# 0 40
# dtype: int64
print(s1.value_counts()) #每個值的數量
# 40 2
# 35 1
# 5 1
# 90 1
# 10 1
# dtype: int64
數學運算
import numpy as np
print(s1.values)
# [10 40 5 90 35 40]
print(s1 / 2) #對每個值除2
# 0 5.0
# 1 20.0
# 2 2.5
# 3 45.0
# 4 17.5
# 5 20.0
# dtype: float64
print(s1 // 2) #對每個值除2後取整
# 0 5
# 1 20
# 2 2
# 3 45
# 4 17
# 5 20
# dtype: int64
print(s1 % 2) #取餘
# 0 0
# 1 0
# 2 1
# 3 0
# 4 1
# 5 0
# dtype: int64
print(s1 ** 2) #求平方
# 0 100
# 1 1600
# 2 25
# 3 8100
# 4 1225
# 5 1600
# dtype: int64
print(np.sqrt(s1)) #求開方
# 0 3.162278
# # 1 6.324555
# # 2 2.236068
# # 3 9.486833
# # 4 5.916080
# # 5 6.324555
# # dtype: float64
print(np.log(s1)) #求對數
# 0 2.302585
# 1 3.688879
# 2 1.609438
# 3 4.499810
# 4 3.555348
# 5 3.688879
# dtype: float64
對齊計算
d2 = pd.Series({'a': 10, 'b': 40, 'c': 5, 'd': 90, 'e': 35, 'f': 40}, name='數值')
d3 = pd.Series({'a': 10, 'b': 20, 'd': 23, 'g': 90, 'h': 35, 'i': 40}, name='數值')
d4 = d2 + d3
print(d4)
# a 20.0
# b 60.0
# c NaN
# d 113.0
# e NaN
# f NaN
# g NaN
# h NaN
# i NaN
# Name: 數值, dtype: float64
2.5 缺失值處理
notnull()
找空值isnull()
找非空值fillna()
填充空值
print(d4[d4.notnull()])
# a 20.0
# b 60.0
# d 113.0
# Name: 數值, dtype: float64
print(d4[d4.isnull()])
# c NaN
# e NaN
# f NaN
# g NaN
# h NaN
# i NaN
# Name: 數值, dtype: float64
d4=d4.fillna(d4.median())
print(d4)
# a 20.0
# b 60.0
# c 60.0
# d 113.0
# e 60.0
# f 60.0
# g 60.0
# h 60.0
# i 60.0
# Name: 數值, dtype: float64
2.6 刪除值
print(d4.drop('b'))
# a 20.0
# c 60.0
# d 113.0
# e 60.0
# f 60.0
# g 60.0
# h 60.0
# i 60.0
# Name: 數值, dtype: float64
參考文獻
- https://blog.csdn.net/MsSpark/article/details/83050261
- https://tianchi.aliyun.com/course/courseConsole?spm=5176.12282070.0.0.2c372042hvOdQn&courseId=41138&chapterIndex=4§ionIndex=1
往期活動
LSGO軟件技術團隊會定期開展提升編程技能的刻意練習活動,希望大家能夠參與進來一起刻意練習,一起學習進步!
- 我是如何組織“算法刻意練習活動”的?
- Day01 兩數相加
- Day02 尋找兩個有序數組的中位數
- Day03 最長迴文子串
- Day04 整數反轉
- Day05 字符串轉換整數 (atoi)
- Day06 迴文數
- Day07 盛最多水的容器
- Day08 最長公共前綴
- Day09 三數之和
- Day10 最接近的三數之和
- Day11 有效的括號
- Day12 合併兩個有序鏈表
- Day13 合併K個排序鏈表
- Day14 刪除排序數組中的重複項
- Day15 搜索旋轉排序數組
- Day16 字符串相乘
- Day17 全排列
- Day18 最大子序和
- Day19 螺旋矩陣
- Day20 螺旋矩陣 II
- Day21 旋轉鏈表
- Day22 不同路徑
- Day23 爬樓梯
- Day24 子集
- Day25 合併兩個有序數組
- Day26 格雷編碼
- Day27 二叉樹的最大深度
- Day28 買賣股票的最佳時機
- Day29 買賣股票的最佳時機 II
- Day30 二叉樹中的最大路徑和
- Day31 只出現一次的數字
- Day32 環形鏈表
- Day33 環形鏈表 II
- Day34 LRU緩存機制
- Day35 排序鏈表
- Day36 最小棧
- Day37 相交鏈表
- Day38 求衆數
- Day39 反轉鏈表
- Day40 數組中的第K個最大元素
- Day41 存在重複元素
- Day42 二叉搜索樹中第K小的元素
- Day43 2的冪
- Day44 二叉搜索樹的最近公共祖先
- Day45 二叉樹的最近公共祖先
- Day46 刪除鏈表中的節點
- Day47 除自身以外數組的乘積
- Day48 Nim 遊戲
- Day49 反轉字符串
- Day50 反轉字符串中的單詞 III
我是 終身學習者“老馬”,一個長期踐行“結伴式學習”理念的 中年大叔。
我崇尚分享,渴望成長,於2010年創立了“LSGO軟件技術團隊”,並加入了國內著名的開源組織“Datawhale”,也是“Dre@mtech”、“智能機器人研究中心”和“大數據與哲學社會科學實驗室”的一員。
願我們一起學習,一起進步,相互陪伴,共同成長。
後臺回覆「搜搜搜」,隨機獲取電子資源!
歡迎關注,請掃描二維碼: