Change language

Python | Pandas Series.str.partition ()

|

Pandas str.partition() works similarly to

Example # 1: splitting a string into a list

In this example, the Name column is stripped when & # 39;, & # 39; first appears. The expansion parameter is stored False to expand it into a list instead of a data frame.

# pandas module import

import pandas as pd

 
# create data frame

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/chicago.csv " )

 
# remove null values, if any, to avoid errors

data.dropna (how = ’ all’ , inplace = True )

 
# display the top 5 rows of data
data.head ()

 
# splitting into & # 39;, & # 39; add to list

data [ "Name" ] = data [ "Name" ]. str . partition ( "," , False )

 
# display
data

Output:
As shown in the output image , the "Name" column was split into a list when the "," first appeared. As you can see, the character & # 39;, & # 39; also stored as a separate list item.

Note: Do not confuse the two commas in the list, the one — element and the other — element separator.

Example # 2: Splitting a row into a data frame

In this example, First and Last name are separated from the First name column and stored in separate columns in the data frame.

# pandas module import

import pandas as pd

 
# create data frame

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/chicago .csv " )

  
# remove null values, if any, to avoid errors

data.dropna (how = ’all’ , inplace = True )

 
# display the top 5 rows of data
data.head ()

 
# splitting into & # 39;, & # 39; per data frame

new = data [ "Name" ]. str . partition ( "," , True )

 
# create a separate name column from new data frame

data [ "First Name" ] = new [ 2 ]

 
# create a separate last name column from new data frame

data [ "Last Name" ]   = new [ 0 ]

 
# Remove old Name columns

data.drop (columns = [ " Name " ], inplace = True )

 
# df display
data

Output:
As shown in the output image, the Name column has been split into a 3-column data frame (one of the following: line before comma and line after comma). The dataframe was then used to create new columns in the same dataframe. The "Old Name" column has been removed using the .drop () method.

New data frame

Data frame with added columns

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method