Menu Close

How to Convert a Tuple to a DataFrame in Python

convert a tuple to a dataframe

In this article, we will see how to convert a tuple to a DataFrame in Python with the help of various examples. In our previous tutorials, we have seen lots of Python programs that are very useful for you.In this guide, we demonstrate you, how you can convert python tuple to DataFrame with the help of various examples so that you don’t have any confusion regarding of how to convert a tuple to DataFrame in Python.

But, before further this article, let’s see little about Python tuple and DataFrame.

What is Python Tuple?

A tuple is a core data structure in Python that stored multiple items in an ordered way. Python tuple is ordered, immutable or unchangeable, and also it is able to store duplicate values.
Python tuple always starts and ends with a circular bracket ( ). As you can see below example.

Example

tpl = ("Python", "Machine Learning", "Data Science", "Data Analyst", "Data Analyst")

What is DataFrame?

A DataFrame in Python is a 2-dimensional tabular data structure with rows and columns. Basically, A Dataframe consists of three parts, rows, columns, and data.
Python provides a library named pandas which are used to work with Dataframes. As you can see below examples.

Example


                  0
0            Python
1  Machine Learning
2      Data Science
3      Data Analyst

Now, It’s time to convert python tuple to DataFrame using Python pandas library.

How to convert a tuple to a DataFrame in Python?

Here we will use the Python pandas library to convert Python tuple to DataFrame using various examples.

Example: Python program to convert a tuple to a DataFrame


# Python program to convert tuple to dataframe
from pandas import DataFrame as df

tpl = ("Python", "Machine Learning", "Data Science", "Data Analyst")
# make dataframe
result = df(data=tpl)
print(result)

Output


                  0
0            Python
1  Machine Learning
2      Data Science
3      Data Analyst

DataFrame uses by default indexes ( rows ) and column names but you can pass your own indexes.

Example: Python program to convert a tuple to a DataFrame


tpl = ("Python", "Machine Learning", "Data Science", "Data Analyst")
# make dataframe with indexes and column name
result = df(tpl, index=["a", "b", "c", "d"], columns=["Languages"])
print(result)

Output


          Languages
a            Python
b  Machine Learning
c      Data Science
d      Data Analyst

Example: Python program to convert two tuples to DataFrame


tpl1 = ("Python", "Machine Learning", "Data Science", "Data Analyst")
tpl2 = ("1991", "1952", "1985", "1960")

result = df(tuple(zip(tpl1, tpl2)), index=["a", "b", "c", "d"], columns=["Languages", "Introduced"])
print(result)

Output


          Languages Introduced
a            Python       1991
b  Machine Learning       1952
c      Data Science       1985
d      Data Analyst       1960

Example: Python program to access DataFrame values

Python Tutorials

Here we are going to use the DataFrame values parameter which returns only the values of DataFrame.


tpl1 = ("Python", "Machine Learning", "Data Science", "Data Analyst")
tpl2 = ("1991", "1952", "1985", "1960")

result = df(tuple(zip(tpl1, tpl2)), index=["a", "b", "c", "d"], columns=["Languages", "Introduced"])

# Access the value of dataframe
print(result.values)

Output


[['Python' '1991']
 ['Machine Learning' '1952']
 ['Data Science' '1985']
 ['Data Analyst' '1960']]

Example: Python program to access DataFrame axes

Here we are going to use the DataFrame axes parameter which returns only the axes of DataFrame.


tpl1 = ("Python", "Machine Learning", "Data Science", "Data Analyst")
tpl2 = ("1991", "1952", "1985", "1960")

result = df(tuple(zip(tpl1, tpl2)), index=["a", "b", "c", "d"], columns=["Languages", "Introduced"])

# Access the value of dataframe
print(result.axes)

Output

[Index(['a', 'b', 'c', 'd'], dtype='object'), Index(['Languages', 'Introduced'], dtype='object')]

Conclusion

So, here we have seen all about how to convert a tuple to a DataFrame in Python using various ways. As a developer, you might be required to convert the python tuple to DataFrame, Then you can follow all the above processes.

I hope this article will helpful for you. If you like this article, please share and keep visiting for further Python tutorials.

Ref:- Click Here

Thanks for reading …

Previous Article

Related Posts