Search code examples
pythonstringpandasfindsubstring

How to just keep the substring before the first blank character in one column?


Here is my data sample:

a=pd.DataFrame({'ID':[1,2,3,4,5],
                'Str':['aa aafae afre ht4','v fef 433','1234334 a','bijf 049tu0q4g vie','aaa 1']})

Now I want to just keep the substring before the first blank character. I can find the location of the first blank character. But I do not know how to do the next part.

I'm trying to print the first letters of the first three words of a sentence, but in the d4 = y.find(" ", d3) part, the program doesn't identify it as an integer, and if I convert it to an integer, it causes an error because I'm in base 10.

How do I solve the problem?


Solution

  • You can use

    a['Str'].split()[0] 
    

    to get the first substring before space.