How can I split a column of tuples in a Pandas dataframe?

Given a pandas dataframe, we have to split a column of tuples in it. By Pranit Sharma Last updated : October 06, 2023

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.

The tuple is a built-in data type of python used to store heterogeneous elements. The tuple is considered a series or collection and we can perform operations like insert, update, and delete with its elements.

Split a column of tuples in a Pandas dataframe

To split a column of tuples in pandas, we need to use .tolist() method along with the column of the dataframe.

Let us understand with the help of an example,

Python program to split a column of tuples in a Pandas dataframe

# Importing pandas package
import pandas as pd

# Creating two list of tuples
data = [
    ('Ram', 'APPLE', 23),
    ('Shyam', 'GOOGLE', 25),
    ('Seeta', 'GOOGLE', 22),
    ('Geeta', 'MICROSOFT', 24),
    ('Raman', 'GOOGLE', 23),
    ('Sahil', 'SAMSUNG', 23)
    ]

# Creating a DataFrame
df = pd.DataFrame(data,columns=['Name','Company','Age'])

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

# Splitting columns
res = df['Company'].tolist()

# Display Result
print("Result:\n",res)

Output

The output of the above program is:

Example: How can I split a column of tuples in a Pandas dataframe?frame

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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