How can a dataframe be created using a dictionary of Series in Python?


Dataframe is a two dimensional data structure, where data is stored in a tabular format, in the form of rows and columns.

It can be visualized as an SQL data table or an excel sheet representation. It can be created using the following constructor −

pd.Dataframe(data, index, columns, dtype, copy)

Let us understand how a dataframe can be created using a dictionary of Series.

  • Series is a one dimensional data structure present in the ‘Pandas’ library.

  • The axis label is collectively known as index.

  • Series structure can store any type of data such as integer, float, string, python objects, and so on.

Let us see an example −

Example

 Live Demo

import pandas as pd
my_data = {'ab' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'mn' : pd.Series([56, 78, 13, 13], index=['a', 'b', 'c', 'd'])}
my_df = pd.DataFrame(my_data)
print("The dataframe created from dictionary of series : ")
print(my_df)

Output

The dataframe created from dictionary f series :
   ab   mn
a  1.0  56
b  2.0  78
c  3.0  13
d  NaN  13

Explanation

  • The required libraries are imported, and given alias names for ease of use.

  • Dictionary values consisting of key and value is created, wherein a value is actually a series data structure.

  • This dictionary of series is later passed as a parameter to the ‘Dataframe’ function present in the ‘pandas’ library

  • The dataframe is created by passing the dictionary of series as parameters to it.

  • The dataframe is printed on the console.

Note − The word ‘NaN’ refers to ‘Not a Number’, which means that specific [row,col] value doesn’t have any valid entry.

Updated on: 10-Dec-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements