
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
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
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.
- Related Articles
- Explain how a dataframe structure can be created using list of dictionary values in Python?
- Explain how series data structure in Python can be created using dictionary and explicit index values?
- How can a new column be created to a dataframe using the already present columns in Python?
- How can series be created using Numpy and passing index value explicitly in Python?
- Explain how series data structure in Python can be created using scalar/constant values?
- How can a column of a dataframe be deleted in Python?
- How to create a pandas DataFrame using a dictionary?
- How can a sequential model be created incrementally with Tensorflow in Python?
- How can bubble charts be created using Matplotlib?
- How to create a Pandas series from a python dictionary?
- How can a new column be added to an existing dataframe in Python?
- How can grid plot in Bokeh library be created with Python?
- How many ways a String object can be created in java?
- How to convert a DataFrame into a dictionary in Pandas?
- Python - How to rename multiple column headers in a Pandas DataFrame with Dictionary?
