[ACCEPTED]-Pandas delete parts of string after specified character inside a dataframe-pandas

Accepted answer
Score: 15

You can reformat the values by passing a 2 reformatting function into the apply method as 1 follows:

from StringIO import StringIO
import pandas as pd

data = """   obs  a  b  c  d
1   1-23-12  1  2  3
2  12-23-13  4  5  5
3  21-23-14  4  5  5"""

# Build dataframe from data
df = pd.read_table(StringIO(data), sep='  ')

# Reformat values for column a using an unnamed lambda function
df['a'] = df['a'].apply(lambda x: x.split('-')[0])

This gives you your desired result:

   obs   a  b  c  d
0    1   1  1  2  3
1    2  12  4  5  5
2    3  21  4  5  5

More Related questions