Splitting a pandas dataframe column by delimiter

Use the below:

df['allele'] = [x.split('-')[-1] for x in df['V']]

The above first part retains any values after the '-' sign

df['V'] = [x.split('-')[-0] for x in df['V']]

The above second part retains any values before the '-' sign and automatically replaces the main column

df.head(3)

For storing data into a new dataframe use the same approach, just with the new dataframe:

tmpDF = pd.DataFrame(columns=['A','B'])
tmpDF[['A','B']] = df['V'].str.split('-', expand=True)

Eventually (and more usefull for my purposes) if you would need get only a part of the string value (i.e. text before '-'), you could use .str.split(...).str[idx] like:

df['V'] = df['V'].str.split('-').str[0]
df
    ID      V       Prob
0   3009    IGHV7   1.0000
1   129     IGHV7   1.0000
2   119     IGHV6   0.8000
3   120     GHV6    0.8056

- splits 'V' values into list according to separator '-' and stores 1st item back to the column


Use vectoried str.split with expand=True:

In [42]:
df[['V','allele']] = df['V'].str.split('-',expand=True)
df

Out[42]:
      ID    Prob      V allele
0   3009  1.0000  IGHV7   B*01
1    129  1.0000  IGHV7   B*01
2    119  0.8000  IGHV6   A*01
3    120  0.8056   GHV6   A*01
4    121  0.9000  IGHV6   A*01
5    122  0.8050  IGHV6   A*01
6    130  1.0000  IGHV4   L*03
7   3014  1.0000  IGHV4   L*03
8    266  0.9970  IGHV5   A*01
9    849  0.4010  IGHV5   A*04
10   174  1.0000  IGHV6   A*02
11   844  1.0000  IGHV6   A*02

Tags:

Python

Pandas