import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
对象创建
创建一个series通过传递值得列表,让pandas创建一个默认得整数索引
s=pd.Series([1,3,5,np.nan,6,8])
s
0 1.0
1 3.0
2 5.0
3 NaN
4 6.0
5 8.0
dtype: float64
#DataFrame通过传递带有日期时间索引和标记列得numpy数组创建
dates=pd.date_range('20130101',periods=6)
dates
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
'2013-01-05', '2013-01-06'],
dtype='datetime64[ns]', freq='D')
df=pd.DataFrame(np.random.rand(6,4),index=dates,columns=list('ABCD'))
df
2013-01-010.0025090.3058060.7494110.015479
2013-01-020.6428160.1269930.3798500.099668
2013-01-030.8174320.6920700.7730930.401504
2013-01-040.3149290.6678720.3187770.975938
2013-01-050.8721480.6668280.5162990.046083
2013-01-060.4801560.3089530.0441420.480998
colunms=['A','B','C','D']
df=pd.DataFrame(np.random.rand(6,4),index=dates,columns=colunms)
df
当然,DataFrame通过传递可以转换为类似系列的对象的dict来创建。
In [10]: 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 [11]: df2
Out[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








暂无数据