快速入門pandas擴充套件庫(上)

格蘭芬多的貓頭鷹發表於2020-11-19

快速入門pandas擴充套件庫

  1. 匯入 Pandas 與 NumPy:
In [1]: import numpy as np
In [2]: import pandas as pd

2.生成物件

用值列表生成 Series 時,Pandas 預設自動生成整數索引

In [3]: s = pd.Series([1, 3, 5, np.nan, 6, 8])
In [4]: s
Out[4]:
0 1.0
1 3.0
2 5.0
3 NaN
4 6.0
5 8.0
dtype: float64

用含日期時間索引與標籤的 NumPy 陣列生成 DataFrame:

In [5]: dates = pd.date_range('20130101', periods=6)
In [6]: dates
Out[6]:
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
 '2013-01-05', '2013-01-06'],
 dtype='datetime64[ns]', freq='D')
In [7]: df = pd.DataFrame(np.random.randn(6, 4), index=dates,
columns=list('ABCD'))
In [8]: df
Out[8]: 
 A B C D
2013-01-01 0.469112 -0.282863 -1.509059 -1.135632
2013-01-02 1.212112 -0.173215 0.119209 -1.044236
2013-01-03 -0.861849 -2.104569 -0.494929 1.071804
2013-01-04 0.721555 -0.706771 -1.039575 0.271860
2013-01-05 -0.424972 0.567020 0.276232 -1.087401
2013-01-06 -0.673690 0.113648 -1.478427 0.524988

用 Series 字典物件生成 DataFrame:

In [9]: df2 = pd.DataFrame({'A': 1.,
 ...: 'B': pd.Timestamp('20130102'),
 ...: 'C': pd.Series(1, index=list(range(4)),
dtype='float32'),
 ...: 'D': np.array([3] * 4, dtype='int32'),
 ...: 'E': pd.Categorical(["test", "train", "test",
"train"]),
 ...: 'F': 'foo'})
 ...:
In [10]: df2
Out[10]:
 A B C D E F
0 1.0 2013-01-02 1.0 3 test foo
1 1.0 2013-01-02 1.0 3 train foo
2 1.0 2013-01-02 1.0 3 test foo
3 1.0 2013-01-02 1.0 3 train foo

DataFrame 的列有不同資料型別:

In [11]: df2.dtypes
Out[11]:
A float64
B datetime64[ns]
C float32
D int32
E category
F object
dtype: object

3.檢視資料
下列程式碼說明如何檢視 DataFrame 頭部和尾部資料:

In [13]: df.head()
Out[13]:
 A B C D
2013-01-01 0.469112 -0.282863 -1.509059 -1.135632
2013-01-02 1.212112 -0.173215 0.119209 -1.044236
2013-01-03 -0.861849 -2.104569 -0.494929 1.071804
2013-01-04 0.721555 -0.706771 -1.039575 0.271860
2013-01-05 -0.424972 0.567020 0.276232 -1.087401
In [14]: df.tail(3)
Out[14]:
 A B C D
2013-01-04 0.721555 -0.706771 -1.039575 0.271860
2013-01-05 -0.424972 0.567020 0.276232 -1.087401
2013-01-06 -0.673690 0.113648 -1.478427 0.524988

顯示索引與列名:

In [15]: df.index
Out[15]:
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
 '2013-01-05', '2013-01-06'],
 dtype='datetime64[ns]', freq='D')
In [16]: df.columns
Out[16]: Index(['A', 'B', 'C', 'D'], dtype='object')

DataFrame.to_numpy() 輸出底層資料的 NumPy 物件。注意,DataFrame 的列由多種資料型別組成時,該操作
耗費系統資源較大,這也是 Pandas 和 NumPy 的本質區別:NumPy 陣列只有一種資料型別,DataFrame 每列
的資料型別各不相同。呼叫 DataFrame.to_numpy() 時,Pandas 查詢支援 DataFrame 裡所有資料型別的
NumPy 資料型別。還有一種資料型別是 object,可以把 DataFrame 列裡的值強制轉換為 Python 物件。
下面的 df 這個 DataFrame 裡的值都是浮點數,DataFrame.to_numpy() 的操作會很快,而且不復制資料。

In [17]: df.to_numpy()
Out[17]:
array([[ 0.4691, -0.2829, -1.5091, -1.1356],
 [ 1.2121, -0.1732, 0.1192, -1.0442],
 [-0.8618, -2.1046, -0.4949, 1.0718],
 [ 0.7216, -0.7068, -1.0396, 0.2719],
 [-0.425 , 0.567 , 0.2762, -1.0874],
 [-0.6737, 0.1136, -1.4784, 0.525 ]])

df2 這個 DataFrame 包含了多種型別,DataFrame.to_numpy() 操作就會耗費較多資源。

In [18]: df2.to_numpy()
Out[18]:
array([[1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'test', 'foo'],
 [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'train', 'foo'],
 [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'test', 'foo'],
 [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'train', 'foo']],
dtype=object)

DataFrame.to_numpy() 的輸出不包含行索引和列標籤。
describe() 可以快速檢視資料的統計摘要:`

In [19]: df.describe()
Out[19]:
 A B C D
count 6.000000 6.000000 6.000000 6.000000
mean 0.073711 -0.431125 -0.687758 -0.233103
std 0.843157 0.922818 0.779887 0.973118
min -0.861849 -2.104569 -1.509059 -1.135632
25% -0.611510 -0.600794 -1.368714 -1.076610
50% 0.022070 -0.228039 -0.767252 -0.386188
75% 0.658444 0.041933 -0.034326 0.461706
max 1.212112 0.567020 0.276232 1.071804

轉置資料:

In [20]: df.T
Out[20]:
 2013-01-01 2013-01-02 2013-01-03 2013-01-04 2013-01-05 2013-01-06
A 0.469112 1.212112 -0.861849 0.721555 -0.424972 -0.673690
B -0.282863 -0.173215 -2.104569 -0.706771 0.567020 0.113648
C -1.509059 0.119209 -0.494929 -1.039575 0.276232 -1.478427
D -1.135632 -1.044236 1.071804 0.271860 -1.087401 0.524988

按軸排序:

In [21]: df.sort_index(axis=1, ascending=False)
Out[21]:
 D C B A
2013-01-01 -1.135632 -1.509059 -0.282863 0.469112
2013-01-02 -1.044236 0.119209 -0.173215 1.212112
2013-01-03 1.071804 -0.494929 -2.104569 -0.861849
2013-01-04 0.271860 -1.039575 -0.706771 0.721555
2013-01-05 -1.087401 0.276232 0.567020 -0.424972
2013-01-06 0.524988 -1.478427 0.113648 -0.673690

按值排序:

In [22]: df.sort_values(by='B')
Out[22]:
 A B C D
2013-01-03 -0.861849 -2.104569 -0.494929 1.071804
2013-01-04 0.721555 -0.706771 -1.039575 0.271860
2013-01-01 0.469112 -0.282863 -1.509059 -1.135632
2013-01-02 1.212112 -0.173215 0.119209 -1.044236
2013-01-06 -0.673690 0.113648 -1.478427 0.524988
2013-01-05 -0.424972 0.567020 0.276232 -1.087401

4.選擇

選擇、設定標準 Python / Numpy 的表示式已經非常直觀,互動也很方便,但對於生產程式碼,我們還是
推薦優化過的 Pandas 資料訪問方法:.at、.iat、.loc 和 .iloc。

獲取資料
選擇單列,產生 Series,與 df.A 等效:

In [23]: df['A']
Out[23]:
2013-01-01 0.469112
2013-01-02 1.212112
2013-01-03 -0.861849
2013-01-04 0.721555
2013-01-05 -0.424972
2013-01-06 -0.673690
Freq: D, Name: A, dtype: float64

切片行:

In [24]: df[0:3]
Out[24]:
 A B C D
2013-01-01 0.469112 -0.282863 -1.509059 -1.135632
2013-01-02 1.212112 -0.173215 0.119209 -1.044236
2013-01-03 -0.861849 -2.104569 -0.494929 1.071804
In [25]: df['20130102':'20130104']
Out[25]:
 A B C D
2013-01-02 1.212112 -0.173215 0.119209 -1.044236
2013-01-03 -0.861849 -2.104569 -0.494929 1.071804
2013-01-04 0.721555 -0.706771 -1.039575 0.271860

相關文章