How to Remove Everything After a Delimiter in a Pandas Column String


How can we remove everything after a delimiter in a string of a Pandas DataFrame column?

Example scenario

Suppose we have DataFrame df:

         col
0    A:text1
1    B:text2
2    C:text3
3    D:text4
4    E:text5

We want to modify DataFrame to contain the column col with these values:

   col
0    A
1    B
2    C
3    D
4    E

Remove delimiter using split and str

We can use str to use standard string methods on a Pandas series.

df['col'] = df['col'].str.split(':').str[0]

The str.split() function will give us a list of strings.

[A, text1]
[B, text2]
[C, text3]
[D, text4]
[E, text5]

The str[0] will allow us to grab the first element of the list.

The assignment operator will allow us to update the existing column.