我是python的初学者。我有一个dataframe看起来像这样:

df
No  ID
1   52Device-24-03HAntang
2   40Device-16HAntang


我想拆分ID列。我的预期结果如下所示:

Result
No  ID                      No_ID   Device        Code  Name
1   52Device-24-03HAntang   52      Device-24-03  H     Antang
2   40Device-16HAntang      40      Device-16     H     Antang


谢谢

最佳答案

如果需要按大写字母拆分,请先在Series.str.replace之前添加空格,然后使用Series.str.split将此空格拆分:

cols = ['No_ID','Device','Code','Name']
df[cols] = df['ID'].str.replace(r"([A-Z])", r" \1").str.split(expand=True)
print (df)
   No                     ID No_ID        Device Code    Name
0   1  52Device-24-03HAntang    52  Device-24-03    H  Antang
1   2     40Device-16HAntang    40     Device-16    H  Antang

07-25 21:31
查看更多