str.split()

noun

say it: “S-T-R dot SPLIT” /ˌɛs ti ˈɑr dɑt ˈsplɪt/

A pandas tool that cuts each value of a text column into pieces at a separator, such as a space, and can put the pieces in columns of their own.

What does str.split() do in pandas?

str.split() cuts each value of a pandas text column at a separator, a space unless you name another. expand=True puts the pieces in columns of their own, and n=1 cuts only at the first separator, so a two-word last name stays together.

import pandas as pd
names = pd.Series(["Ana Torres Vega", "Sam Chen"])
parts = names.str.split(" ", n=1, expand=True)
print(parts[1])   # "Torres Vega", "Chen"

In a sentence

  • “str.split() turned the runner_name column into first and last names.”
  • “With n=1, str.split() kept "Torres Vega" together as one last name.”

Level: Journeyman · More Python terms at this level

Updated