JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Split a string into two lists using str.split in Python Pandas

Author:JIYIK Last Updated:2025/04/12 Views:

Pandas has a method to split a string based on a separator/delimiter. We will use the pandas str.split()function.


str.split()Split a string into two lists/columns using the function in Python Pandas

The string can be saved as a list of Series, or a single delimited string, multi-column DataFrame.

The functions used are similar to Python's default split()methods, but they can only be applied to a single string.

grammar:

Syntax:Series.str.split(pat=None, n=-1, expand=False)
Let's define each of the parameters of syntax
Parameters:
pat:String value, separator, or delimiter used to separate strings
n=The maximum number of separations to make in a single string; the default is -1, which signifies all.
expand: If True, this Boolean value returns a data frame with different values in separate columns. Otherwise, it returns a series containing a collection of strings.
return: Depending on the expand parameter, a series of lists or a data frame will be generated.

First, we explain with a simple example, and then a CSV file.


Split a string column in a Pandas DataFrame into multiple columns using basic syntax

data[["A", "B"]] = data["A"].str.split(",", 1, expand=True)

See the examples below, which demonstrate the use of this syntax in practice.

Split the column by comma:

import pandas as pd

df = pd.DataFrame(
    {"Name": ["Anu,Ais ", "Bag, Box", "fox, fix"], "points": [112, 104, 127]}
)
df

Output:

# split team column into two columns
df[["Name", "lastname"]] = df["Name"].str.split(",", 2, expand=True)
df

Output:

For a download of the CSV file used in the code, click here.

The student grades data is contained in the DataFrame used in the following examples. Append the image of the DataFrame before any operations.

We explain string splitting in two ways.

  • Convert a string to a list
  • Creating separate columns from strings

Convert String to List in Python Pandas

The split function is used in this data dto split the lunch column at each . The option is set to 1, and the maximum number of separations in a single string is 1.

The expand parameter is set to False. Instead of a Series of DataFrames, a list of strings is returned.

import pandas as pd

df = pd.read_csv("/content/drive/MyDrive/StudentsPerformance.csv")
# dropping null value columns to avoid errors
df.dropna(inplace=True)
# new data frame with split value columns
df["lunch"] = df["lunch"].str.split("d", n=1, expand=True)
# df display
df.head(9)

Output:

The output image shows that the lunch column now has one list because the n option is set to 1.

dThe string is delimited at the first occurrence , not at subsequent occurrences (maximum 1 delimiter in the string).


Creating separate columns from strings in Python Pandas

In this example, the parent education columns are " "separated by spaces and the expand option is set to True.

This means it will return a DataFrame with all the separated strings in different columns. The new column is then constructed using the Dataframe.

Use drop()the method to remove the old parent education level column.

import pandas as pd

df = pd.read_csv("/content/drive/MyDrive/StudentsPerformance.csv")
# dropping null value columns to avoid errors
df.dropna(inplace=True)
new = df["parental level of education"].str.split(" ", n=1, expand=True)
df["educational level"] = new[0]
df["insititute"] = new[1]
# Dropping old Name columns
df.drop(columns=["parental level of education"], inplace=True)
# df display
df.head(9)

Output:

split()The function provides a new DataFrame which creates two new columns (Education Level and College) in the DataFrame.

The above image shows the new columns. We can also view the new columns using the new keyword which shows the newly created columns.

new.head(9)

Output:


in conclusion

Therefore, there is usually a section in your Pandas infobox that needs to be split into two parts in the info outline.

For example, if one part of your information outline is your full name, you may want to separate it into first name and last name.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Finding the installed version of Pandas

Publish Date:2025/04/12 Views:190 Category:Python

Pandas is one of the commonly used Python libraries for data analysis, and Pandas versions need to be updated regularly. Therefore, other Pandas requirements are incompatible. Let's look at ways to determine the Pandas version and dependenc

KeyError in Pandas

Publish Date:2025/04/12 Views:81 Category:Python

This tutorial explores the concept of KeyError in Pandas. What is Pandas KeyError? While working with Pandas, analysts may encounter multiple errors thrown by the code interpreter. These errors are wide ranging and can help us better invest

Grouping and Sorting in Pandas

Publish Date:2025/04/12 Views:90 Category:Python

This tutorial explored the concept of grouping data in a DataFrame and sorting it in Pandas. Grouping and Sorting DataFrame in Pandas As we know, Pandas is an advanced data analysis tool or package extension in Python. Most of the companies

Plotting Line Graph with Data Points in Pandas

Publish Date:2025/04/12 Views:65 Category:Python

Pandas is an open source data analysis library in Python. It provides many built-in methods to perform operations on numerical data. Data visualization is very popular nowadays and is used to quickly analyze data visually. We can visualize

Converting Timedelta to Int in Pandas

Publish Date:2025/04/12 Views:123 Category:Python

This tutorial will discuss converting a to a using dt the attribute in Pandas . timedelta int Use the Pandas dt attribute to timedelta convert int To timedelta convert to an integer value, we can use the property pandas of the library dt .

Pandas fill NaN values

Publish Date:2025/04/12 Views:93 Category:Python

This tutorial explains how we can use DataFrame.fillna() the method to fill NaN values ​​with specified values. We will use the following DataFrame in this article. import numpy as np import pandas as pd roll_no = [ 501 , 502 , 503 , 50

Pandas Convert String to Number

Publish Date:2025/04/12 Views:147 Category:Python

This tutorial explains how to pandas.to_numeric() convert string values ​​of a Pandas DataFrame into numeric type using the method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 , 103 , 343 , 565 ], "Name" :

How to Change the Data Type of a Column in Pandas

Publish Date:2025/04/12 Views:139 Category:Python

We will look at methods for changing the data type of columns in a Pandas Dataframe, as well as options like to_numaric , , as_type and infer_objects . We will also discuss how to to_numaric use downcasting the option in . to_numeric Method

Get the first row of Dataframe Pandas

Publish Date:2025/04/12 Views:78 Category:Python

This tutorial explains how to use the get_first_row pandas.DataFrame.iloc attribute and pandas.DataFrame.head() get_first_row method from a Pandas DataFrame. We will use the following DataFrame in the following example to explain how to get

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial