1,当指定某列为索引的时候,我们仍然可以用行数:使用irow()、icol()方法
例子:df.irow(0) 来获取第0行
2、利用DataFrame中的列做列表解析时候,需要强制类型转换:
age = [2016 - int(str(df.loc[i,'证件号码'])[6:10]) for i in df.index ] 虽然 df.loc[i,'证件号码'] type的时候是字符型,但放到列表解析的时候貌似不是了,所以必须转换一下
3、 身份证变为号码还有另外一种方法:
from operator import mod
mod(int(df.证件号码[i].replace('X','0'))%100000000,10000)
4、关于看某位置是否为空值时候需要注意:
np.isnan( 这里的类型可以是链表,或者array类型),对pandas 的Series类型不可用,但是可以进行 array类型转换
Series类型看时候是空值 用 isnull()函数,例如:df['证件号码'].isnull().ix[i] == False
5、删掉有空值的列
6、用pandas画直方图
7、判断字符串为空 s.isspace()
8、关于随机
(1)state = random.choice(list(range(10))) #在0-9中随机选择一个值
(2)sampler = np.random.permutation(5) #0-4随机排序,
然后把这个放进reindex中
df.reindex(sampler)
9、随机抽样
(1)在‘数袋子’里面 随机抽取
>>> bag = np.array([5,7,-1,6,4])>>> sampler = np.random.randint(0,len(bag),size=10)>>> bag.take(sampler)array([5, 5, 7, 5, 7, 5, 6, 7, 7, 4])>>> bag[sampler]array([5, 5, 7, 5, 7, 5, 6, 7, 7, 4])
(2)在一个序列里面随机选出 number个
slice = random.sample(list(range(10)), 3)#number=3
联系客服