Numpy 和 Pandas 有什么不同?
如果用 python 的列表和字典來作比較, 那么可以說 Numpy 是列表形式的,沒有數(shù)值標(biāo)簽,而 Pandas 就是字典形式。Pandas是基于Numpy構(gòu)建的,讓Numpy為中心的應(yīng)用變得更加簡單。
pandas基本功能和使用方法有哪些?
要使用pandas,首先需要了解他主要兩個數(shù)據(jù)結(jié)構(gòu):Series和DataFrame。
Demo.py
import pandas as pd
import numpy as np
#Series的創(chuàng)建:
#Series的字符串表現(xiàn)形式為:索引在左邊,值在右邊。由于我們沒有為數(shù)據(jù)指定索引。
#于是會自動創(chuàng)建一個0到N-1(N為長度)的整數(shù)型索引。
s = pd.Series([1,3,6,np.nan,44,1])
print(s)
#DataFrame 的創(chuàng)建:
#DataFrame是一個表格型的數(shù)據(jù)結(jié)構(gòu),它包含有一組有序的列,
#每列可以是不同的值類型(數(shù)值,字符串,布爾值等)。
#DataFrame既有行索引也有列索引, 它可以被看做由Series組成的大字典。
dates = pd.date_range('20160101',periods=6)
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=['a','b','c','d'])
print(df)
#DataFrame 的一些簡單運用
#我們可以根據(jù)每一個不同的索引來挑選數(shù)據(jù), 比如挑選 b 的元素:
print df['b']
#創(chuàng)建一組沒有給定行標(biāo)簽和列標(biāo)簽的數(shù)據(jù) df1
df1 = pd.DataFrame(np.arange(12).reshape((3,4)))
print(df1)
# 還有一種生成 df 的方法, 如下 df2:
#這種方法能對每一列的數(shù)據(jù)進行特殊對待
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)
#查看數(shù)據(jù)中的類型, 可以用 dtype 這個屬性:
print(df2.dtypes)
#查看對列的序號:
print(df2.index)
#查看每種數(shù)據(jù)的名稱:
print(df2.columns)
#只想查看所有df2的值:
print(df2.values)
#想知道數(shù)據(jù)的總結(jié), 可以用 describe()
print df2.describe()
#想翻轉(zhuǎn)數(shù)據(jù), transpose:
print(df2.T)
#想對數(shù)據(jù)的 index 進行排序并輸出
print(df2.sort_index(axis=1, ascending=False))
#對數(shù)據(jù)值排序輸出:
print(df2.sort_values(by='B'))
結(jié)果:
0 1.0
1 3.0
2 6.0
3 NaN
4 44.0
5 1.0
dtype: float64
a b c d
2016-01-01 0.883398 0.033547 -0.394937 -0.337354
2016-01-02 0.132106 1.057983 -1.007236 -0.311857
2016-01-03 1.255509 1.284663 -0.534759 -0.291661
2016-01-04 1.119228 -0.363833 0.877059 -1.313416
2016-01-05 0.356150 1.431102 1.671152 0.836090
2016-01-06 -0.070478 0.421939 0.591910 0.213282
2016-01-01 0.033547
2016-01-02 1.057983
2016-01-03 1.284663
2016-01-04 -0.363833
2016-01-05 1.431102
2016-01-06 0.421939
Freq: D, Name: b, dtype: float64
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
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
A float64
B datetime64[ns]
C float32
D int32
E category
F object
dtype: object
Int64Index([0, 1, 2, 3], dtype='int64')
Index([u'A', u'B', u'C', u'D', u'E', u'F'], dtype='object')
[[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']]
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
0 1 2 \
A 1 1 1
B 2013-01-02 00:00:00 2013-01-02 00:00:00 2013-01-02 00:00:00
C 1 1 1
D 3 3 3
E test train test
F foo foo foo
3
A 1
B 2013-01-02 00:00:00
C 1
D 3
E train
F foo
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
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