- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Difference between data frames and matrices in Python Pandas?

In this article, we will show you the differences between dataframe and matrices in python pandas.

Both dataframes and matrices are 2-dimensional data structures. In general, dataframes can include **multiple** types of data (numeric, character, factor, etc) while matrices can only store **one **type of data.

## Dataframe in Python

In Python, a DataFrame is a two-dimensional, tabular, mutable data structure that may store tabular data containing objects of various data types. A DataFrame has axes that are labeled in the form of rows and columns. DataFrames are useful tools in data preprocessing because they provide valuable data handling methods. DataFrames can also be used to create pivot tables and plot data with Matplotlib.

### Applications of Dataframe

Data frames can perform a variety of tasks, such as fit statistical formulas.

Data processing (Not possible with Matrix, first converting to Data Frame is mandatory)

Transposing rows to columns and vice versa is feasible, which is useful in Data Science.

## Creating a sample data frame

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

Use the import keyword, to import the

**pandas, numpy**module with alias names.Create a dataframe using the

**DataFrame()**function of the pandas module.Print the input dataframe.

### Example

The following program returns a dataframe using the DataFrame() function −

# 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

On executing, the above program will generate the following output −

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

## Matrix in Python

Matrix is a homogeneous collection of data sets organised in a two-dimensional rectangular grid. It’s an m*n array with the same data type. It is created with a vector input. There are a fixed number of rows and columns. Python supports numerous arithmetic operations such as addition, subtraction, multiplication, and division on Matrix.

### Applications of Matrix

It is very useful in Economics for calculating statistics such as GDP (Gross Domestic Product) or PI (Price per capita income).

It's also useful for studying electrical and electronic circuits.

Print the input dataframe.

Matrixes are utilized in survey research, such as plotting graphs.

This is useful in probability and statistics.

## Matrix multiplication by converting a matrix to data frame

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

Use the import keyword, to import the

**pandas**module with an alias names.Create two variables to store both the input matrices separately.

Use the

**DataFrame()**function(creates a dataframe)of the pandas module to create a dataframe of both the first and second matrixes and store them in separate variables. Here the data is loaded into a pandas DataFrames.Print the dataframe of input matrix 1.

Print the dimensions(shape) of input matrix 1 by applying the

**shape**attribute to it.Print the dataframe of input matrix 2.

Print the dimensions(shape) of input matrix 2 by applying the

**shape**attribute to it.Use the

**dot()**function to multiply both the matrices inputMatrix_1 and inputMatrix_2 and create a variable to store it.Print the resultant matrix of matrix multiplication of inputMatrix_1 and inputMatrix_2.

Print the dimensions(shape) of the resultant Matrix by applying the shape attribute to it.

### Example

The following program returns a dataframe using the DataFrame() function −

# importing 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]] # creating a dataframe of first matrix #(here data is loaded into a pandas DataFrames) df_1 = pd.DataFrame(data=inputMatrix_1) # creating a dataframe of second matrix df_2 = pd.DataFrame(data=inputMatrix_2) # printing the dataframe of input matrix 1 print("inputMatrix_1:") print(df_1) # printing the dimensions(shape) of input matrix 1 print("The dimensions(shape) of input matrix 1:") print(df_1.shape) print() # printing the dataframe of input matrix 2 print("inputMatrix_2:") print(df_2) # printing the dimensions(shape) of input matrix 1 print("The dimensions(shape) of input matrix 2:") print(df_2.shape) print() # multiplying both the matrices inputMatrix_1 and inputMatrix_2 result_mult = df_1.dot(df_2) # Printing the resultant of matrix multiplication of inputMatrix_1 and inputMatrix_2 print("Resultant Matrix after Matrix multiplication:") print(result_mult) # printing the dimensions(shape) of resultant Matrix print("The dimensions(shape) of Resultant 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 Resultant Matrix: (3, 3)

Below is the differences table of matrix and dataframe.

### Matrix vs Dataframe

Matrix | Dataframe |
---|---|

It is a collection of datasets arranged in a two-dimensional rectangular organization | It stores the data tables with multiple data types in multiple columns called fields. |

The matrix is an m*n array with the same data type | Dataframe is a list of vectors of identical length. A data frame is a generalized form of a matrix. |

A matrix has a fixed number of rows and columns. | Dataframe has a variable number of rows and columns. |

Homogeneous | Heterogeneous |

## Conclusion

We learned about the differences between matrices and data frames in Python in this program. We also learned how to make data frames and convert a matrix to a data frame.