Python資料處理之(十)Pandas 基本介紹
阿新 • • 發佈:2018-11-21
一、Numpy 和 Pandas 有什麼不同
如果用 python
的列表和字典來作比較, 那麼可以說 Numpy
是列表形式的,沒有數值標籤,而 Pandas
就是字典形式。Pandas
是基於Numpy
構建的,讓Numpy
為中心的應用變得更加簡單。
要使用pandas
,首先需要了解他主要兩個資料結構:Series
和DataFrame
。
二、Series
>>> import pandas as pd
>>> import numpy as np
>>> s=pd.Series([1,3,6,np.nan,44, 1])
>>> print(s)
0 1.0
1 3.0
2 6.0
3 NaN
4 44.0
5 1.0
dtype: float64
Series
的字串表現形式為:索引在左邊,值在右邊。由於我們沒有為資料指定索引。於是會自動建立一個0
到N-1
(N
為長度)的整數型索引。
三、DataFrame
在這裡插入程式碼片
四、DataFrame的一些簡單應用
>>> dates=pd.date_range('20181120',periods=6)
>>> print(dates)
DatetimeIndex( ['2018-11-20', '2018-11-21', '2018-11-22', '2018-11-23',
'2018-11-24', '2018-11-25'],
dtype='datetime64[ns]', freq='D')
>>> df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=['a','b','c','d'])
>>> print(df)
a b c d
2018 -11-20 1.110778 1.788520 0.356670 0.507927
2018-11-21 2.082814 -0.465353 2.496047 1.146531
2018-11-22 2.456575 0.444252 0.723751 -1.513549
2018-11-23 0.398651 -0.352025 -0.727106 0.396843
2018-11-24 -0.618329 0.882563 0.716917 0.615774
2018-11-25 1.096260 -0.478230 0.076957 0.135207
>>>
DataFrame
是一個表格型的資料結構,它包含有一組有序的列,每列可以是不同的值型別(數值,字串,布林值等)。DataFrame
既有行索引也有列索引, 它可以被看做由Series
組成的大字典。
我們可以根據每一個不同的索引來挑選資料, 比如挑選 b
的元素:
五、DataFrame 的一些簡單運用
>>> print(df['b'])
2018-11-20 1.788520
2018-11-21 -0.465353
2018-11-22 0.444252
2018-11-23 -0.352025
2018-11-24 0.882563
2018-11-25 -0.478230
Freq: D, Name: b, dtype: float64
我們再建立一組沒有給定行標籤和列標籤的資料 df1
:
>>> df1=pd.DataFrame(np.arange(12).reshape((3,4)))
>>> print(df1)
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
這樣,他就會採取預設的從0開始 index. 還有一種生成 df 的方法, 如下 df2:
>>> 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'})
>>> print(df2)
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
這種方法能對每一列的資料進行特殊對待. 如果想要檢視資料中的型別, 我們可以用 dtype
這個屬性:
>>> print(df2.dtypes)
A float64
B datetime64[ns]
C float32
D int32
E category
F object
如果想看對列的序號:
>>> print(df2.index)
Int64Index([0, 1, 2, 3], dtype='int64')
同樣, 每種資料的名稱也能看到:
>>> print(df2.columns)
Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')
如果只想看所有df2
的值:
>>> print(df2.values)
[[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']]
想知道資料的總結, 可以用 describe():
>>> df2.describe()
A C D
count 4.0 4.0 4.0
mean 1.0 1.0 3.0
std 0.0 0.0 0.0
min 1.0 1.0 3.0
25% 1.0 1.0 3.0
50% 1.0 1.0 3.0
75% 1.0 1.0 3.0
max 1.0 1.0 3.0
如果想翻轉資料, transpose:
>>> print(df2.T)
0 1 2 3
A 1 1 1 1
B 2013-01-02 00:00:00 2013-01-02 00:00:00 2013-01-02 00:00:00 2013-01-02 00:00:00
C 1 1 1 1
D 3 3 3 3
E test train test train
F foo foo foo
如果想對資料的 index 進行排序並輸出:
>>> print(df2.sort_index(axis=1,ascending=False))
F E D C B A
0 foo test 3 1.0 2013-01-02 1.0
1 foo train 3 1.0 2013-01-02 1.0
2 foo test 3 1.0 2013-01-02 1.0
3 foo train 3 1.0 2013-01-02 1.0
如果是對資料 值 排序輸出:
>>> print(df2.sort_values(by='B'))
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