1. zfill ( ) :从前方以零填充至n位数
'123'.zfill(6)
结果:000123
在dataframe中的一列中进行填充:
position2['code'] = position2['code'].apply(lambda x: str(x).zfill(6))
2. lstrip(): 删除前导零
A = ['0001245','0133','1000']
res = [x.lstrip('0') for x in A]
print (str(res))
结果: ['1245','133','1000']
x.rstrip(" "): 删除后面的所有的空格
A = ['1245 ','133 ','1000 ' ]
res = [x.rstrip(" ") for x in A]
结果: ['1245','133','1000']
3. split :分拆
“111_ab”.split("_")
结果:['111', 'ab']
“111_ab”.split("_") [0]
结果:‘111’
4. re.search
[re.search(r"[A-Z]{1,2}",i).group(0) for i in position['code'] ]
改变一列中的每一个数字的格式
stock_mv3['日期'].astype('str')
5. 复制
将string 复制x份
copydata1 = np.array(str(date))
copydata2 = np.repeat(copydata1,x , axis = 0)