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) OutputName 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 DataFramePandas 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) OutputName 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 DataFrameTo 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) OutputName 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 alsoPython 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 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.
Output: Code #2: Using from_records()
Output: Code #3: Using
Output: Code #4: For pivoting it possible.
Output: |