Pandas delete parts of the string after the specified character inside the data frame

I would like a simple mehtod to delete parts of the string after the specified character inside the data frame. Here is a simplified example:

DF:

obs abcd 0 1 1-23-12 1 2 3 1 2 12-23-13 4 5 5 2 3 21-23-14 4 5 5 

I would like to remove the parts in the column after the first character, my expected result:

newdf:

  obs abcd 0 1 1 1 2 3 1 2 12 4 5 5 2 3 21 4 5 5 
+7
python string pandas
source share
1 answer

You can reformat the values ​​by passing the reformat function to the apply method as follows:

 from StringIO import StringIO import pandas as pd data = """ obs abcd 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 will give you the desired result:

  obs abcd 0 1 1 1 2 3 1 2 12 4 5 5 2 3 21 4 5 5 
+13
source share

All Articles