valid
valid

Reputation: 63

Split column in python pandas into two columns

I want to split the entry (always a string containing 6 letters) of a column 'Compared Image Type' of a pandas dataframe into two new columns of which one contains the first three letters, the other contains the last three letters of the original column.

Name    BaseImage Type  Compared Image Type
 2       oldNeg             semNeg
 2       oldNeu             perNeu
 2       oldNeu             semNeu
 2       oldNeu             newNeu
 2       oldNeg             perNeg

So far I've only found out how to split a column after a certain character (e.g. after a ",") and would be thankful for any help.

Upvotes: 0

Views: 688

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

You have str access:

df['col1'] = df['Compared Image Type'].str[:3]
df['col2'] = df['Compared Image Type'].str[3:]

OUtput:

   Name BaseImage Type Compared Image Type col1 col2
0     2         oldNeg              semNeg  sem  Neg
1     2         oldNeu              perNeu  per  Neu
2     2         oldNeu              semNeu  sem  Neu
3     2         oldNeu              newNeu  new  Neu
4     2         oldNeg              perNeg  per  Neg

Based on your data, you can also use a similar approach to split a column using certain character, here capital letters [A-Z]:

df['Compared Image Type'].str.extract('^(\w*)([A-Z]\w*)')

Output:

     0    1
0  sem  Neg
1  per  Neu
2  sem  Neu
3  new  Neu
4  per  Neg

Upvotes: 4

Related Questions