Difference between Dataframe and Matrix in Python Pandas?

What’s the Difference Between Data Frames and Matrices in Python Pandas?

This article will explain the difference between data frames and matrices in Python Pandas.

Both data frames and matrices are two-dimensional data structures. Generally speaking, data frames can contain multiple data types (numeric, character, factor, etc.), while matrices can only store a single data type.

Data Frames in Python

In Python, a data frame is a two-dimensional, tabular, mutable data structure that can store tabular data containing objects of multiple data types. Data frames have axes labeled in rows and columns. Data frames are very useful tools for data preprocessing because they provide valuable data manipulation methods. Data frames can also be used to create pivot tables and plot data using Matplotlib.

Applications of Data Frames

  • Data frames can be used to perform a variety of tasks, such as fitting statistical formulas.
  • Data manipulation (matrices are not supported and must first be converted to data frames).

  • Converting rows to columns and vice versa is very useful in data science.

Creating a Sample Dataframe

Algorithm (Steps)

Below is the algorithm/steps to perform the required task −

  • Using the import keyword, import the pandas and numpy modules with their aliases.
  • Using the DataFrame() function of the pandas module to create a dataframe.

  • Print the input dataframe.

Example

The following program uses the DataFrame() function to return a data frame. −

 # importing pandas, numpy modules with alias names
    import pandas as pd
    import numpy as np

    #creating a dataframe
    inputDataframe = pd. **DataFrame({** 'Name': ['Virat', 'Rohit', 'Meera', 'Nick', 'Sana'], 'Jobrole': ['Developer', 'Analyst', 'Help Desk', 'Database Developer', 'Finance accountant'], 'Age': [25, 30, 28, 25, 40] **})**

# displaying the dataframe
print(inputDataframe)

Output

When executing the above program, the following output will be generated −

 Name Jobrole Age
0 Virat Developer 25
1 Rohit Analyst 30
2 Meera Help Desk 28
3 Nick Database Developer 25
4 Sana Finance Accountant 40

Matrices in Python

A matrix is a collection of homogeneous data sets organized in a two-dimensional rectangular grid. It is an m*n array of the same data type. It is created using vector inputs. It has a fixed number of rows and columns. In Python, matrices support various arithmetic operations such as addition, subtraction, multiplication, and division.

Applications of Matrices

  • In economics, it is very useful for calculating statistics such as Gross Domestic Product or Price per capita income.
  • It is also used in studying electrical circuits and electronic circuits.

  • Printing input data frames.

  • Matrices are used to draw graphs in survey research.

  • This is useful in probability and statistics.

Matrix Multiplication to Convert a Matrix to a Data Frame

Algorithm (Steps)

Below is the algorithm/steps to perform the required task –

  • Use the import keyword to import the pandas module and create an alias.
  • Create two variables to store the input matrices.

  • Use the DataFrame() function (Create Data Frame) in the pandas module to create data frames for the two matrices and store them in separate variables. Here, the data is loaded into a pandas data frame.

  • Print the data frame for input matrix 1.

  • Apply the shape attribute to print the dimensions (shape) of input matrix 1.

  • Print the data frame for input matrix 2.

  • Apply the shape attribute to print the dimensions (shape) of input matrix 2.

  • Use the dot() function to multiply input matrix 1 and input matrix 2 and create a variable to store the resulting matrix.

  • Prints the result matrix of the product of input matrix 1 and input matrix 2.

  • Apply the shape attribute to print the dimensions (shape) of the result matrix.

Example

The following program uses the DataFrame() function to return a data frame.

# Import the pandas module
import pandas as pd

# Input Matrix 1
inputMatrix_1 = [[1, 2, 2],
[1, 2, 0],
[1, 0, 2]]

# Input Matrix 2
inputMatrix_2 = [[1, 0, 1],
[2, 1, 1],
[2, 1, 2]]

# Create a DataFrame for the first matrix
# (Here, load the data into a pandas DataFrame)
df_1 = pd.DataFrame(data=inputMatrix_1)

# Create a DataFrame for the second matrix
df_2 = pd.DataFrame(data=inputMatrix_2)

# Print the DataFrame for Input Matrix 1
print("inputMatrix_1:")
print(df_1)

# Print the dimensions (shape) of input matrix 1
print("The dimensions (shape) of input matrix 1:")
print(df_1.shape)
print()

# Print the data frame of input matrix 2
print("InputMatrix_2:")
print(df_2)

# Print the dimensions (shape) of input matrix 2
print("The dimensions (shape) of input matrix 2:")
print(df_2.shape)
print()

# Multiply input matrix 1 and input matrix 2
result_mult = df_1.dot(df_2)

# Print the result of the matrix multiplication of input matrix 1 and input matrix 2
print("Resultant Matrix after Matrix multiplication:")
print(result_mult)

# Print the dimensions (shape) of the result of the matrix multiplication
print("The dimensions (shape) of the resultant matrix Matrix:")
print(result_mult.shape)

Output

inputMatrix_1:
0 1 2
0 1 2 2
1 1 2 0
2 1 0 2
The dimensions(shape) of input matrix 1:
(3, 3)

inputMatrix_2:
0 1 2
0 1 0 1
1 2 1 1
2 2 1 2
The dimensions(shape) of input matrix 2:
(3, 3)

Resultant Matrix after Matrix multiplication:
0 1 2
0 9 4 7
1 5 2 3
2 5 2 5
The dimensions(shape) of the resulting matrix:
(3, 3)

Below is a table showing the differences between the matrix and the data frame.

Matrix vs. Dataframe

Matrix Dataframe
This is a collection of data sets organized as a two-dimensional rectangle. A data frame stores a table of data with multiple data types, with columns called fields.
A matrix is an m*n array of the same data type. A data frame is a list of vectors of the same length. A data frame is a generalization of a matrix.
A matrix has a fixed number of rows and columns. A data frame has a variable number of rows and columns.
Homogeneous Heterogeneous

Conclusion

In this program, we learned the difference between matrices and data frames in Python. We also learned how to create data frames and convert matrices to data frames.

Leave a Reply

Your email address will not be published. Required fields are marked *