Pandas split column into multiple columns by comma

Given a pandas dataframe, we have to split column into multiple columns by comma.
Submitted by Pranit Sharma, on October 11, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

A string is a group of characters. A string may contain any type of character including numerical characters, alphabetical characters, special characters, etc. Splitting a string means the distribution of the string in two or more parts.

Problem statement

Suppose, we are given a DataFrame with multiple columns and out of these columns one column has some comma-separated values, we need to split these values by a comma.

Splitting column into multiple columns by comma

To split column into multiple columns by comma, we will use the split() method.

By default, a string is split with a space between two words but if we want to split a string with any other character, we need to pass the specific character inside str.split() method. Here, for splitting a string into two different columns, we are going to use str.split() method.

Let us understand with the help of an example,

Python program to split column into multiple columns by comma

# Importing pandas package
import pandas as pd

# Creating two dictionary
d = {
    'Name':['Ram,Sharma', 'Shyam,rawat','Seeta,phoghat','Geeta,phogat'],
    'Age':[20,32,33,19]
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display DataFrames
print("DataFrame1:\n",df,"\n")

# Splitting the column name and storing 
# the first part into another column
df['new'] = df['Name'].str.split(',').str[0]

# Display modified DataFrame
print("Modified DataFrame 1:\n",df)

Output

The output of the above program is:

Example: Pandas split column into multiple columns by comma

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.