python 數據分析工具包 pandas(一)

1. 簡單介紹

pandas 是專爲 python 編程語言設計的高性能,簡單易用的數據結構數據分析工具庫,它創建在 numpy 之上,能夠許多第三方庫完美集成在同一個科學計算環境中。pandas 被普遍應用於金融,統計,社會科學和許多工程技術領域,處理典型數據分析案例。python

2. 安裝

pandas 支持 conda 和 pip 兩種方式安裝。git

conda 安裝:github

conda install pandas
複製代碼

pip 安裝:編程

pip install pandas
複製代碼

截至本文寫做之時,官方最新發布版本是 v0.25.1,發佈時間爲2019年8月22日。最新版本是 0.25.x 系列的bug修復版,建議更新。更新方式以下:數組

pip install --upgrade pandas
複製代碼

3. 數據結構

pandas 有兩種主要的數據結構:Series(1維)和 DataFrame (2維)。bash

下面分別介紹這兩種數據結構,首先在咱們的 python 腳本或 jupyter notebook 中導入 pandas,業界慣例縮寫爲 pd。微信

import pandas as pd
複製代碼

3.1 Series

Series 是一維標記數組,可以保存任何數據類型(整數,字符串,浮點數,Python對象等)。 軸標籤統稱爲索引。數據結構

3.1.1 建立 Series

經過列表建立:編程語言

data = [1, 2, 3]
pd.Series(data)
複製代碼
0    1
1    2
2    3
dtype: int64
複製代碼

經過字典建立:工具

data = {'a': 1, 'b': 2, 'c': 3}
pd.Series(data)
複製代碼
a    1
b    2
c    3
dtype: int64
複製代碼

經過 index 參數設置索引(標籤):

data = [1, 2, 3]
index = ['a', 'b', 'c']
pd.Series(data, index=index)
複製代碼
a    1
b    2
c    3
dtype: int64
複製代碼

經過標量建立(相同值),並設置索引(標籤,不能重複):

data = 0
index = ['a', 'b', 'c']
pd.Series(data, index=index)
複製代碼
a    0
b    0
c    0
dtype: int64
複製代碼

3.1.2 訪問 Series

s = pd.Series([10, 100, 1000], index=['a', 'b', 'c'])
s
複製代碼
a      10
b     100
c    1000
dtype: int64
複製代碼

數組方式訪問:

print(s[0], s[1], s[2])
複製代碼
10 100 1000
複製代碼

字典方式訪問:

print(s['a'], s['b'], s['c'])
複製代碼
10 100 1000
複製代碼

可見兩種訪問方式之間的對應關係:

print(s[0] == s['a'], s[1] == s['b'], s[2] == s['c'])
複製代碼
True True True
複製代碼

3.2 DataFrame

DataFrame 是一個二維標記數據結構,具備可能不一樣類型的列。 能夠將其類比於電子表格或 SQL 表,或 Series 對象的字典。 它也是最經常使用的 pandas 對象。

3.2.1 建立 DataFrame

經過列表字典建立:

data = {
    'col1': [1, 2, 3],
    'col2': [4, 5, 6],
    'col3': [7, 8, 9]
}

pd.DataFrame(data)
複製代碼
col1 col2 col3
0 1 4 7
1 2 5 8
2 3 6 9

經過 Series 字典建立:

s1 = pd.Series([1, 2, 3], index=['row1', 'row2', 'row3'])
s2 = pd.Series([4, 5, 6], index=['row2', 'row3', 'row4'])
s3 = pd.Series([7, 8, 9], index=['row3', 'row4', 'row5'])

data = {
    'col1': s1,
    'col2': s2,
    'col3': s3
}

pd.DataFrame(data)
複製代碼
col1 col2 col3
row1 1.0 NaN NaN
row2 2.0 4.0 NaN
row3 3.0 5.0 7.0
row4 NaN 6.0 8.0
row5 NaN NaN 9.0

經過字典列表建立:

data = [
    {'col1': 1, 'col2': 2, 'col3': 3},
    {'col1': 2, 'col2': 3, 'col3': 4},
    {'col1': 3, 'col2': 4, 'col3': 5}
]

pd.DataFrame(data, index=['row1', 'row2', 'row3'])
複製代碼
col1 col2 col3
row1 1 2 3
row2 2 3 4
row3 3 4 5

經過二維列表建立:

data = [
    [1, 2, 3],
    [2, 3, 4],
    [3, 4, 5]
]

pd.DataFrame(data, index=['row1', 'row2', 'row3'], columns=['col1', 'col2', 'col3'])
複製代碼
col1 col2 col3
row1 1 2 3
row2 2 3 4
row3 3 4 5

3.2.2 訪問 DataFrame

df = pd.DataFrame([[1, 4, 7], [2, 5, 8], [3, 6, 9]],
                  index=['row1', 'row2', 'row3'],
                  columns=['col1', 'col2', 'col3'])
df
複製代碼
col1 col2 col3
row1 1 4 7
row2 2 5 8
row3 3 6 9

經過列標籤訪問列:

df['col1']
複製代碼
row1    1
row2    2
row3    3
Name: col1, dtype: int64
複製代碼

經過行標籤訪問行:

df.loc['row1']
複製代碼
col1    1
col2    4
col3    7
Name: row1, dtype: int64
複製代碼

經過整數訪問行:

df.iloc[0]
複製代碼
col1    1
col2    4
col3    7
Name: row1, dtype: int64
複製代碼

經過切片選擇行:

df[1:]
複製代碼
col1 col2 col3
row2 2 5 8
row3 3 6 9

3.2.3 轉置 DataFrame

將行列互換,相似線性代數中矩陣的轉置。

df.T
複製代碼
row1 row2 row3
col1 1 2 3
col2 4 5 6
col3 7 8 9

猜你喜歡


堅持寫專欄不易,若是以爲本文對你有幫助,記得點個贊。感謝支持!


微信掃描二維碼 獲取最新技術原創

相關文章
相關標籤/搜索