AppDividend
Latest Code Tutorials

How to Convert Python Tuple to DataFrame

0

List in Python has a collection of arbitrary objects. Tuple has an ordered collection of objects. Tuples are represented by enclosing the items in parentheses (( )) instead of square brackets ([ ]). Unlike the list, tuples are immutable.

Convert Python Tuple to DataFrame

To convert a Python tuple to DataFrame, use the list of tuples and pass that list to a pd.DataFrame() constructor, and it will return a DataFrame. Pandas DataFrame is a two-dimensional, size-mutable, heterogeneous tabular data structure that contains rows and columns.

The DataFrame requires rows and columns, and we can provide the column names manually, but we need data to create a row. To create rows, we will use the list of tuples, and hence we will create a DataFrame from tuples.

import pandas as pd

data = [('Facebook', 750, True),
        ('Alphabet', 1100, True),
        ('Amazon', 1700, True),
        ('Apple', 2100, False),
        ('Microsoft', 1750, False)]

df = pd.DataFrame(data, columns=['Name', 'M-cap', 'Internet Companies'])

print(df)

Output

        Name  M-cap  Internet Companies
0   Facebook    750                True
1   Alphabet   1100                True
2     Amazon   1700                True
3      Apple   2100               False
4  Microsoft   1750               False

First, we imported the Pandas module and then defined a list of tuples and then pass that list of tuples to the DataFrame constructor in addition to columns, and it returns the DataFrame.

Using from_records() method to create a DataFrame

Pandas from_records() method creates a DataFrame object from a structured ndarray, tuples, dictionaries, or DataFrame.

import pandas as pd

data = [('Facebook', 750, True),
        ('Alphabet', 1100, True),
        ('Amazon', 1700, True),
        ('Apple', 2100, False),
        ('Microsoft', 1750, False)]

df = pd.DataFrame.from_records(
    data, columns=['Name', 'M-cap', 'Internet Companies'])
print(df)

Output

        Name  M-cap  Internet Companies
0   Facebook    750                True
1   Alphabet   1100                True
2     Amazon   1700                True
3      Apple   2100               False
4  Microsoft   1750               False

In this example, we are using the from_records() method of pandas and pass the data and columns, and it returns the DataFrame.

Convert Python tuple of tuples to DataFrame

To convert the Python tuple of tuples into DataFrame, transform the tuple of tuples into a list of tuples and then use the DataFrame constructor to convert a list of tuples into DataFrame.

import pandas as pd

data = (('Facebook', 750, True),
        ('Alphabet', 1100, True),
        ('Amazon', 1700, True),
        ('Apple', 2100, False),
        ('Microsoft', 1750, False))

dataList = list(data)

df = pd.DataFrame(
    dataList, columns=['Name', 'M-cap', 'Internet Companies'])
print(df)

Output

        Name  M-cap  Internet Companies
0   Facebook    750                True
1   Alphabet   1100                True
2     Amazon   1700                True
3      Apple   2100               False
4  Microsoft   1750               False

And we get the exact DataFrame. Converting List to DataFrame is a straightforward task, so if we convert any data type to list somehow, it will be straightforward to create a DataFrame from the list.

That is it for converting Python Tuple to DataFrame.

See also

Python tuple to json

Python tuple to set

Python tuple to string

Python tuple to array

Python tuple to dictionary

Python tuple to list

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.