Pandas快速入門

Pandas是一個開源的Python的數據分析庫,BSD許可的庫,為Python編程語言提供高性能,易於使用的數據結構和數據分析工具。

發現有很多想要學習Python卻不知道如何下手的朋友,我這裡整理了一些關於Python的學習資料,從基礎到入門到實戰都有!有需要的朋友可以關注並私信“01”免費獲取...

二、為什麼要用Pandas ?

Python在數據處理和準備方面一直做得很好,但在數據分析和建模方面就沒那麼好了。Pandas幫助填補了這一空白,使您能夠在Python中執行整個數據分析工作流程

Python與Pandas在廣泛的學術和商業領域中使用,包括金融,神經科學,經濟學,統計學,廣告,網絡分析,等等。

Pandas快速入門

三、入門Pandas

1. 通常,我們按如下方式導入 Pandas:

In [1]: import numpy as npIn [2]: import pandas as pd

2. 對象創建

通過傳入一些值的列表來創建一個Series,Pandas會自動創建一個默認的整數索引:

s = pd.Series([1,3,5,np.nan,2,4,6])
print(s)
0 1.0
1 3.0
2 5.0
3 NaN
4 2.0
5 4.0
6 6.0
dtype: float64

通過傳遞帶有日期時間索引和帶標籤列的NumPy數組來創建DataFrame

df = pd.DataFrame(np.random.randn(6, 4), index=pd.date_range('20190901', periods=6), columns=list('ABCD'))

print(df)

A B C D

2019-09-01 -1.661250 1.008909 0.192640 -0.146610

2019-09-02 -0.215992 -0.113884 0.365838 -0.253161

2019-09-03 -2.875189 -0.454827 -0.084804 1.436878

2019-09-04 -0.310008 -0.958814 1.371260 2.326361

2019-09-05 0.458977 1.834737 1.086609 -1.714873

2019-09-06 0.995078 -0.458551 1.844880 1.654410

Pandas快速入門

3. 查看數據

DataFrame

 A B C D
2019-09-01 -1.661250 1.008909 0.192640 -0.146610
2019-09-02 -0.215992 -0.113884 0.365838 -0.253161
2019-09-03 -2.875189 -0.454827 -0.084804 1.436878
2019-09-04 -0.310008 -0.958814 1.371260 2.326361
2019-09-05 0.458977 1.834737 1.086609 -1.714873
2019-09-06 0.995078 -0.458551 1.844880 1.654410

查看DataFrame頂部和尾部的數據:

print(df.head(1)) #查看前面N行
print(df.tail(1)) #查看後面N行
print(df.columns) #查看列索引
print(df.to_numpy()) #只顯示數據,不顯示行、列索引
A B C D
2019-09-01 -1.66125 1.008909 0.19264 -0.14661
A B C D
2019-09-06 0.995078 -0.458551 1.84488 1.65441
Index(['A', 'B', 'C', 'D'], dtype='object')
[[-1.66124953 1.00890873 0.19263997 -0.14661048]
[-0.2159917 -0.11388431 0.36583777 -0.25316099]
[-2.87518903 -0.45482652 -0.08480368 1.4368784 ]
[-0.31000762 -0.95881398 1.37126015 2.32636067]
[ 0.45897748 1.83473729 1.08660874 -1.71487318]
[ 0.99507797 -0.45855055 1.84488027 1.65440963]]
Pandas快速入門

4. 對象旋轉

print(df.T) #行列轉制數據
print(df.sort_index(axis=1,ascending=False)) #列轉制數據
print(df.sort_index(ascending=False)) #行轉制數據
2019-09-01 2019-09-02 2019-09-03 2019-09-04 2019-09-05 2019-09-06
A -1.661250 -0.215992 -2.875189 -0.310008 0.458977 0.995078
B 1.008909 -0.113884 -0.454827 -0.958814 1.834737 -0.458551
C 0.192640 0.365838 -0.084804 1.371260 1.086609 1.844880
D -0.146610 -0.253161 1.436878 2.326361 -1.714873 1.654410
D C B A
2019-09-01 -0.146610 0.192640 1.008909 -1.661250
2019-09-02 -0.253161 0.365838 -0.113884 -0.215992
2019-09-03 1.436878 -0.084804 -0.454827 -2.875189
2019-09-04 2.326361 1.371260 -0.958814 -0.310008
2019-09-05 -1.714873 1.086609 1.834737 0.458977
2019-09-06 1.654410 1.844880 -0.458551 0.995078
A B C D
2019-09-06 0.995078 -0.458551 1.844880 1.654410
2019-09-05 0.458977 1.834737 1.086609 -1.714873
2019-09-04 -0.310008 -0.958814 1.371260 2.326361
2019-09-03 -2.875189 -0.454827 -0.084804 1.436878
2019-09-02 -0.215992 -0.113884 0.365838 -0.253161
2019-09-01 -1.661250 1.008909 0.192640 -0.146610
5. 對象選擇
print(df.sort_values(by='B')) #選擇B列,按B列值從小到大排序
print(df.iloc[:3]) #df[行,列],取前3行
print(df.iloc[:,:3]) #df[行,列],取前3列
print(df.loc['2019-09-04':'2019-09-06']) #df[行,列],按行索引取值
print(df.loc[:,'A':'C']) #df[行,列],按列索引取值
A B C D
2019-09-04 -0.310008 -0.958814 1.371260 2.326361
2019-09-06 0.995078 -0.458551 1.844880 1.654410
2019-09-03 -2.875189 -0.454827 -0.084804 1.436878
2019-09-02 -0.215992 -0.113884 0.365838 -0.253161
2019-09-01 -1.661250 1.008909 0.192640 -0.146610
2019-09-05 0.458977 1.834737 1.086609 -1.714873
A B C D
2019-09-01 -1.661250 1.008909 0.192640 -0.146610
2019-09-02 -0.215992 -0.113884 0.365838 -0.253161

2019-09-03 -2.875189 -0.454827 -0.084804 1.436878
A B C
2019-09-04 -0.310008 -0.958814 1.371260
2019-09-06 0.995078 -0.458551 1.844880
2019-09-03 -2.875189 -0.454827 -0.084804
2019-09-02 -0.215992 -0.113884 0.365838
2019-09-01 -1.661250 1.008909 0.192640
2019-09-05 0.458977 1.834737 1.086609
A B C D
2019-09-04 -0.310008 -0.958814 1.371260 2.326361
2019-09-05 0.458977 1.834737 1.086609 -1.714873
2019-09-06 0.995078 -0.458551 1.844880 1.654410


分享到:


相關文章: