Hướng dẫn python tuple to dataframe

Hướng dẫn python tuple to dataframe

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. Pandas DataFrame is a two-dimensional, size-mutable, heterogeneous tabular data structure that contains rows and columns.

To convert a Python tuple to DataFrame, use the pd.DataFrame() constructor that accepts a tuple as an argument and it returns a DataFrame. 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 passed that list of tuples to the DataFrame constructor in addition to columns, and it returned the DataFrame.

Using from_records() method to create a DataFrame

Pandas from_records() is a library method that 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 use 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 a tuple of tuples into DataFrame in Python, convert a tuple of tuples into a list of tuples and 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 somehow convert any data type to a list, 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

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    We can create a DataFrame from a list of simple tuples, and can even choose the specific elements of the tuples we want to use.

    Code #1: Simply passing tuple to DataFrame constructor.

    import pandas as pd

    data = [('Peter', 18, 7),

            ('Riff', 15, 6),

            ('John', 17, 8),

            ('Michel', 18, 7),

            ('Sheli', 17, 5) ]

    df = pd.DataFrame(data, columns =['Name', 'Age', 'Score'])

    print(df) 

    Output:

    Hướng dẫn python tuple to dataframe

     
    Code #2: Using from_records()

    import pandas as pd

    data = [('Peter', 18, 7),

            ('Riff', 15, 6),

            ('John', 17, 8),

            ('Michel', 18, 7),

            ('Sheli', 17, 5) ]

    df = pd.DataFrame.from_records(data, columns =['Team', 'Age', 'Score'])

    print(df) 

    Output:

    Hướng dẫn python tuple to dataframe

     

    Code #3: Using from_items()

    import pandas as pd

    data = [

    ('Age', [18, 15, 17, 18, 17]),

    ('Team', ['A', 'B', 'A', 'C', 'B']),

    ('Score', [7, 6, 8, 7, 5]),

    ]

    df = pd.DataFrame.from_items(data)

    print(df) 

    Output:

    Hướng dẫn python tuple to dataframe

     
    Code #4: For pivoting it possible.

    import pandas as pd

    data = [('Peter', 18, 7),

            ('Riff', 15, 6),

        ('John', 17, 8),

        ('Michel', 18, 7),

        ('Sheli', 17, 5) ]

    df = pd.DataFrame(data, columns =['Team', 'Age', 'Score'])

    a = df.pivot('Team', 'Score','Age')

    print(a)

    Output:

    Hướng dẫn python tuple to dataframe