File: nth_word.rst

package info (click to toggle)
pandas 2.2.3%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 66,784 kB
  • sloc: python: 422,228; ansic: 9,190; sh: 270; xml: 102; makefile: 83
file content (9 lines) | stat: -rw-r--r-- 439 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
The simplest way to extract words in pandas is to split the strings by spaces, then reference the
word by index. Note there are more powerful approaches should you need them.

.. ipython:: python

   firstlast = pd.DataFrame({"String": ["John Smith", "Jane Cook"]})
   firstlast["First_Name"] = firstlast["String"].str.split(" ", expand=True)[0]
   firstlast["Last_Name"] = firstlast["String"].str.rsplit(" ", expand=True)[1]
   firstlast