a b c d
2018-01-07 -0.005483 -0.006776 1.410138 -0.447714
2018-01-08 -0.070957 1.444326 -0.152151 -0.129219
print df['20180107':'20180110']
a b c d
2018-01-07 -0.005483 -0.006776 1.410138 -0.447714
2018-01-08 -0.070957 1.444326 -0.152151 -0.129219
2018-01-09 0.371654 -0.052031 1.520965 0.697403
2018-01-10 -0.940761 0.339332 0.305061 -1.076497
#通过标签选择:locprint df.loc['20180110']
a -0.940761
b 0.339332
c 0.305061
d -1.076497
Name: 2018-01-10 00:00:00, dtype: float64
#选择a和b的所有列数据print df.loc[:,['a','b']]
a b
2018-01-07 -0.005483 -0.006776
2018-01-08 -0.070957 1.444326
2018-01-09 0.371654 -0.052031
2018-01-10 -0.940761 0.339332
2018-01-11 0.854794 -0.477876
2018-01-12 0.906029 -0.493668
#通过位置选择:ilocprint df.iloc[3]
a -0.940761
b 0.339332
c 0.305061
d -1.076497
Name: 2018-01-10 00:00:00, dtype: float64
print df.iloc[3,1]
0.33933171911
print df.iloc[1:3,2:3]
c
2018-01-08 -0.152151
2018-01-09 1.520965
print df.iloc[[1,3,5],2:3]
c
2018-01-08 -0.152151
2018-01-10 0.305061
2018-01-12 -1.983745
#综合选择(mix loc and iloc):ixprint df.ix[:3,['a','b']]
a b
2018-01-07 -0.005483 -0.006776
2018-01-08 -0.070957 1.444326
2018-01-09 0.371654 -0.052031
#是或否删选(Boolean indexing)print df
a b c d
2018-01-07 -0.005483 -0.006776 1.410138 -0.447714
2018-01-08 -0.070957 1.444326 -0.152151 -0.129219
2018-01-09 0.371654 -0.052031 1.520965 0.697403
2018-01-10 -0.940761 0.339332 0.305061 -1.076497
2018-01-11 0.854794 -0.477876 -0.776903 1.134447
2018-01-12 0.906029 -0.493668 -1.983745 -1.260175
print df[df.a>0]
a b c d
2018-01-09 0.371654 -0.052031 1.520965 0.697403
2018-01-11 0.854794 -0.477876 -0.776903 1.134447
2018-01-12 0.906029 -0.493668 -1.983745 -1.260175
#总共4中删选方式
#赋值
df.iloc[3,3] = 11print df
a b c d
2018-01-07 -0.005483 -0.006776 1.410138 -0.447714
2018-01-08 -0.070957 1.444326 -0.152151 -0.129219
2018-01-09 0.371654 -0.052031 1.520965 0.697403
2018-01-10 -0.940761 0.339332 0.305061 11.000000
2018-01-11 0.854794 -0.477876 -0.776903 1.134447
2018-01-12 0.906029 -0.493668 -1.983745 -1.260175
df.a[df.a<0] = 0print df
a b c d
2018-01-07 0.000000 -0.006776 1.410138 -0.447714
2018-01-08 0.000000 1.444326 -0.152151 -0.129219
2018-01-09 0.371654 -0.052031 1.520965 0.697403
2018-01-10 0.000000 0.339332 0.305061 11.000000
2018-01-11 0.854794 -0.477876 -0.776903 1.134447
2018-01-12 0.906029 -0.493668 -1.983745 -1.260175
#添加一列
df['e'] = pd.Series([1,2,3,4,5,6])
print df
a b c d e
2018-01-07 0.000000 -0.006776 1.410138 -0.447714 NaN
2018-01-08 0.000000 1.444326 -0.152151 -0.129219 NaN
2018-01-09 0.371654 -0.052031 1.520965 0.697403 NaN
2018-01-10 0.000000 0.339332 0.305061 11.000000 NaN
2018-01-11 0.854794 -0.477876 -0.776903 1.134447 NaN
2018-01-12 0.906029 -0.493668 -1.983745 -1.260175 NaN