如果用 python 的列表和字典来作比较, 那么可以说 Numpy 是列表形式的,没有数值标签,而 Pandas 就是字典形式。Pandas是基于Numpy构建的,让Numpy为中心的应用变得更加简单。
使用pandas的两个数据结构:Series和DataFrame。
Series
import pandas as pd
import numpy as np
s = pd.Series([1,2,3,np.nan,44,1])
print (s)
print (type(s)) # <class 'pandas.core.series.Series'>
"""
#输出 s的数据结构和numpy的区别是多了序列,更像是key-value的形式
0 1.0
1 2.0
2 3.0
3 NaN
4 44.0
5 1.0
dtype: float64
"""
DataFrame
dates = pd.date_range('20160101',periods=6)
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=['a','b','c','d'])
print(df)
print (type(df))
"""
a b c d
2016-01-01 1.215386 0.666379 -0.946796 -1.081178
2016-01-02 0.290950 -0.091997 0.493116 1.343718
2016-01-03 -0.589404 0.039176 0.302297 -0.505347
2016-01-04 0.680195 -0.204143 -0.828407 1.078363
2016-01-05 -1.828872 -0.377683 0.510895 -0.695981
2016-01-06 -0.035956 0.768245 0.846849 0.535861
<class 'pandas.core.frame.DataFrame'>
"""
网友评论