0

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.

valid
  • 63
  • 6
  • 1
    `df['Compared Image Type'].str.slice(0, 3)` and `df['Compared Image Type'].str.slice(3)` – Erfan Mar 02 '20 at 15:36
  • Does this answer your question? [Pandas make new column from string slice of another column](https://stackoverflow.com/questions/25789445/pandas-make-new-column-from-string-slice-of-another-column) – AMC Mar 02 '20 at 17:30

1 Answers1

4

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
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74