
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Python Tuple into a Two-Dimensional Table
A tuple in Python is an ordered, immutable collection that stores multiple items. It supports mixed data types, allows indexing, and is commonly used for fixed data structures.
What is a Two-Dimensional Table?
A two-dimensional table is a way of specifying data in rows and columns, similar to a spreadsheet or a table in a Word doc. Each row represents a record, and each column represents a specific property for that record.
Example
The following is a two-dimensional table consisting of three rows and three columns. It is structured along two axes: vertical rows and horizontal columns, This makes the data organized and more accessible.
NAME | AGE | CITY |
---|---|---|
Ramesh | 32 | Hyderabad |
Suresh | 42 | Bangalore |
Rajesh | 52 | Chennai |
This Python code imports pandas, creates a DataFrame from a list containing names, age, and cities then prints the structured table format for data analysis and visualization.
import pandas as pd data = [ ["Ramesh", 32, "Hyderabad"], ["Suresh", 42, "Bangalore"], ["Rajesh", 52, "Chennai"] ] df = pd.DataFrame(data, columns=["Name", "Age", "City"]) print(df)
We will get the result as follows -
Name Age City 0 Ramesh 32 Hyderabad 1 Suresh 42 Banglore 2 Rajesh 52 Chennai
Transforming a Python Tuple into a Table
To convert a Python tuple into a two-dimensional table, we must reshape it using libraries like NumPy or pandas. Use numpy.reshape() or pandas.DataFrame() for structured table representation.
Tuple of Tuple
Tuple is Flat
Tuple of Tuple
Here, the code defines a tuple of tuples data, representing a matrix. It converts each tuple into a list, storing them in a table.
data = ( (1, 2, 3), (4, 5, 6), (7, 8, 9) ) table = [list(row) for row in data] print(table)
The output is produced as follows -
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Tuple of Flat
This Python code creates a two-dimensional table from a tuple. It defines row and column counts, the tuple converts them into a list, and prints the resulting table structure.
t = (1, 2, 3, 4, 5, 6) rows = 2 cols = 3 table = [list(t[i:i+cols]) for i in range(0, len(t), cols)] print(table)
The result is obtained as follows -
[[1, 2, 3], [4, 5, 6]]
The reshape() method converts a tuple into a multidimensional array. The following example imports NumPy, generates a 1D array from 1 to 9, and then reshapes it into a 3*3 matrix, and prints the output.
import numpy data = numpy.array(range(1,10)) data.reshape([3,3]) print(data)
This will give the output as -
[1 2 3 4 5 6 7 8 9]
Example
This code creates a tuple of data containing numbers from 1 to 9. Then, it splits data into sub-tuples of three elements each using list comprehension and stores them in a table.
data = tuple(range(1, 10)) table = tuple(data[n:n+3] for n in range(0, len(data), 3)) print(table)
We will get the output as follows -
((1, 2, 3), (4, 5, 6), (7, 8, 9))