Coding Ref

How to use str.split() in Pandas

How to use str.split() in Pandas

To use the str.split() method in Pandas, you can use the str attribute of a Pandas series, followed by the split() method.

This method allows you to split each string in the series into a list of substrings, based on a specified separator.

Example

Here's an example of using the str.split() method in Pandas:

main.py
import pandas as pd

# create a sample series
s = pd.Series(["apple, banana, orange, pear"])

# split each string in the series into a list of substrings
# using the comma character as the separator
s_split = s.str.split(",")

# display the result
print(s_split)

This will split each string in the series into a list of substrings, using the comma character as the separator. The resulting list of substrings will be displayed to the console.

The output will be:

output
0    [apple,  banana,  orange,  pear]
dtype: object

Conclusion

The str.split() method is a convenient way to split a string into a list of substrings.

It allows you to easily split each string in a Pandas series into a list of substrings, using a specified separator.

This can be useful for working with the data in the substrings.

You'll also like

Related tutorials curated for you

    How to print a specific row in a Pandas DataFrame?

    How to round in Pandas

    How to convert a Pandas Index to a List

    How to create a bar chart in Pandas

    What is categorical data in Pandas?

    What is insert() in Pandas?

    What does factorize() do in Pandas?

    How to reorder columns in Pandas

    What is Pandas Cumsum()?

    How to create a freqeuncy table in Pandas

    How to shuffle data in Pandas

    How to reset index in a Pandas DataFrame