2 回答
TA贡献1859条经验 获得超6个赞
df['REFERENCE_ID'] = df['PRECERT_ID'].astype(str)
# Save all uppercase english letters in a list
letters = list(string.ascii_uppercase)
# Enumerate over the letters list and start with 11 as the OP wants in this way only.
# All the uppercase english letters and corresponding numbers starting with 11.
for i,l in enumerate(letters, start=11):
df.loc[df['REFERENCE_ID'].str.startswith(str(i)), 'REFERENCE_ID'] = l + df['PRECERT_ID'].str[-7:]
TA贡献1829条经验 获得超7个赞
我会尝试制作一本字典并用来map加快速度。
要创建查找字典,您可以使用:
lu_dict = dict(zip([str(i) for i in range(11,20)],[chr(i) for i in range(65,74)]))
返回:
{'11': 'A',
'12': 'B',
'13': 'C',
'14': 'D',
'15': 'E',
'16': 'F',
'17': 'G',
'18': 'H',
'19': 'I'}
然后你可以使用它.str.slice.map来避免 for 循环。
df = pd.DataFrame(data = {'Reference_ID':['112326345','12223356354','6735435634']})
df.Reference_ID = df.Reference_ID.astype(str)
df.loc[:,'Reference_new'] = df.Reference_ID.str.slice(0,2).map(lu_dict) + df.Reference_ID.str.slice(-7, )
结果是:
Reference_ID Reference_new
0 112326345 A2326345
1 12223356354 B3356354
2 6735435634 NaN
添加回答
举报