技術圖文:Pandas的簡單入門教程:Series

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&sectionIndex=1

往期活動

LSGO軟件技術團隊會定期開展提升編程技能的刻意練習活動,希望大家能夠參與進來一起刻意練習,一起學習進步!


我是 終身學習者“老馬”,一個長期踐行“結伴式學習”理念的 中年大叔

我崇尚分享,渴望成長,於2010年創立了“LSGO軟件技術團隊”,並加入了國內著名的開源組織“Datawhale”,也是“Dre@mtech”、“智能機器人研究中心”和“大數據與哲學社會科學實驗室”的一員。

願我們一起學習,一起進步,相互陪伴,共同成長。

後臺回覆「搜搜搜」,隨機獲取電子資源!
歡迎關注,請掃描二維碼:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章