写给刚接触pandas的小白们,深一点的可以看这里。这也是我的第一篇博客,希望大家多多指教~
1、在使用前,我们需要对相应模块就行导入(也就是import)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
2、创建对象
通过传递一个列表(list)的值创建序列(series),让pandas创建一个默认的整数索引
s=pd.Series([1,3,5,np.nan,6,8])
s
out:
0 1.0
1 3.0
2 5.0
3 NaN
4 6.0
5 8.0
dtype: float64
通过传递具有日期时间索引和标签列的numpy数组来创建一个DataFrame:
datas=pd.data_range('20130101',periods=6)
dates
Out:
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.randn(6,4), index=dates, columns=list('ABCD'))
In [9]: df
Out[9]:
A B C D
2013-01-01 0.469112 -0.282863 -1.509059 -1.135632
2013-01-02 1.212112 -0.173215 0.119209 -1.044236
2013-01-03 -0.861849 -2.104569 -0.494929 1.071804
2013-01-04 0.721555 -0.706771 -1.039575 0.271860
2013-01-05 -0.424972 0.567020 0.276232 -1.087401
2013-01-06 -0.673690 0.113648 -1.478427 0.524988
通过传递一个可以转换为一系列对象的字典来创建一个DataFrame
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' })
})