1、嵌套字典传给DataFrame时,外层字典的键作为列,内层键作为行索引。
>>> pop={'nevada':{2001:2.4,2002:2.9},'ohio':{2000:1.5,2001:1.7,2002:3.6}}
>>> frame3=pd.DataFrame(pop)
>>> frame3
nevada ohio
2000 NaN 1.5
2001 2.4 1.7
2002 2.9 3.6
2、利用标签的切片运算与普通的Python切片运算不同,其末端是包含的(封闭空间)
>>> obj=pd.Series(np.arange(4),index=['a','b','c','d'])
>>> obj
a 0
b 1
c 2
d 3
dtype: int32
>>> obj['b':'c']
b 1
c 2
dtype: int32
>>> obj[2:4]
c 2
d 3
dtype: int32
3、lambda可以定义一个匿名函数
>>> frame=pd.DataFrame(np.random.randn(4,3),columns=list('bde'),index=['utah','ohio','texas','oregon'])
>>> frame
b d e
utah -2.154869 0.258531 0.259937
ohio 1.141130 -0.176782 -0.096375
texas -1.250416 1.082767 -0.590113
oregon -0.017656 -0.274536 -0.805041
>>> f=lambda x:x.max()-x.min()
>>> frame.apply(f)
b 3.295999
d 1.357303
e 1.064978
dtype: float64
4、若要按值对Series进行排序,order方法用于python2版本,python3用sort_values()方法。
5、loc:通过行标签索引行数据;iloc:通过行号索引行数据;ix:通过行标签或者行号索引行数据
>>> df=pd.DataFrame(np.random.randn(4,3),index=['a','a','b','b'])
>>> df
0 1 2
a 0.101699 -0.899057 2.059372
a 1.982451 -0.310547 -1.294779
b -0.319136 -0.291461 -0.272351
b 0.586648 1.485952 0.462408
>>> df.ix['b']
0 1 2
b -0.319136 -0.291461 -0.272351
b 0.586648 1.485952 0.462408
>>> df.loc['b']
0 1 2
b -0.319136 -0.291461 -0.272351
b 0.586648 1.485952 0.462408
>>> df.iloc[0]
0 0.101699
1 -0.899057
2 2.059372
Name: a, dtype: float64
6、describe方法
>>> df=pd.DataFrame([[1.4,np.nan],[7.1,-4.5],[np.nan,np.nan],[0.75,-1.3]],index=['a','b','c','d'],columns=['one','two'])
>>> df.describe()
one two
count 3.000000 2.000000
mean 3.083333 -2.900000
std 3.493685 2.262742
min 0.750000 -4.500000
25% 1.075000 -3.700000
50% 1.400000 -2.900000
75% 4.250000 -2.100000
max 7.100000 -1.300000
#count非NA值的数量;mean值的平均数;std样本值的标准差;min,max最小最大值;25%,50%,75%是f分位数,例50%即中位数