我正在清理pandas数据框中的数据,我想将一列拆分为另一列。
我想按“ eNBID”列拆分“ id”列,但不知道如何拆分
import pandas as pd
id_list = ['4600375067649','4600375077246','460037495681','460037495694']
eNBID_list = ['750676','750772','749568','749569']
df=pd.DataFrame({'id':id_list,'eNBID':eNBID_list})
df.head()
id eNBID
4600375067649 750676
4600375077246 750772
460037495681 749568
460037495694 749569
What I want:
df.head()
id eNBID
460-03-750676-49 750676
460-03-750772-46 750772
460-03-749568-1 749568
460-03-749569-4 749569
#column 'eNBID' is the third part of column 'id', the item length in column 'eNBID' is 6 or 7.
答案 0 :(得分:1)
考虑46003
对于所有ID均保持不变
df['id'] = df.apply(lambda x: '-'.join([i[:3]+'-'+i[3:] if '460' in i else i for i in list(re.findall('(\w*)'+'('+x.eNBID+')'+'(\w*)',x.id)[0])]), axis=1)
输出
id eNBID
0 460-03-750676-49 750676
1 460-03-750772-46 750772
2 460-03-749568-1 749568
3 460-03-749569-4 749569
答案 1 :(得分:0)
在第3、5、11位之后考虑“-”:
df['id'] = df['id'].apply(lambda s: s[:3] + '-' + s[3:5] + '-' + s[5:11] + '-' + s[11:])