'How can I substring to specific character in pandas?

For example, I have 2 columns(1,2), and in table 2 I want to fetch everything until " character.

I wanted to do something like this:

df.columns = ['1','2']

a = df['2'].str[:' " ']

print(a)

but is not possible since I need a number

    column 2 example

[email protected], [email protected]", blah blah
[email protected]", ....
[email protected]", ...
[email protected], [email protected], [email protected]", blah 

return everything until " character.



Solution 1:[1]

Split the string on " and pick the first element.

Use Series.str.split:

df['2'].str.split('"').str[0]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mayank Porwal