Arief Hidayat
Arief Hidayat

Reputation: 967

Split String and Number in Pandas Column

I am a beginner in python. I have a dataframe looks like this:

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

I want to split the ID column. My expected result looks like this:

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

thank you

Upvotes: 2

Views: 922

Answers (1)

jezrael
jezrael

Reputation: 862481

If need split by uppercase letters first add space before uppercase by Series.str.replace and then split by this space with 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

Upvotes: 2

Related Questions