
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Creating DataFrame from dict of narray-lists in Python
Pandas is a very widely used python library for data processing and data analysis. In this article we will see how we we can create pandas dataframe from given python dictionaries and lists.
From dictionary with lists
Dictionaries are key value pairs. If we take a python dictionary which has key and a list as a value then we can directly use the DataFrame method on the given dictionary to create the pandas data frame.
Example
import pandas as pd # Dictionary for Exam Schedule Exam_Schedule = { 'Exam Day': ['Mon', 'Tue', 'Wed','Thu', 'Fri'], 'Exam Subject': ['Chemisry','Physics','Maths','English','Biology'], 'Exam Time': ['2 PM', '10 AM', '11 AM','1 PM', '3 PM'] } # Dictionary to DataFrame Exam_Schedule_df = pd.DataFrame(Exam_Schedule) print(Exam_Schedule_df)
Output
Running the above code gives us the following result −
Exam Day Exam Subject Time 0 Mon Chemisry 2 PM 1 Tue Physics 10 AM 2 Wed Maths 10 AM 3 Thu English 2 PM 4 Fri Biology 10 AM
Adding index
If the data frame is already created, we can add another column to it by adding an index to it. In the below example we take the python dictionary which has exam subjects and exam time. Later we add the exam days as an index to the given data frame.
Example
import pandas as pd # Dictionary for Exam Schedule Exam_Schedule = { 'Exam Subject': ['Chemisry','Physics','Maths','English','Biology'], 'Exam Time': ['2 PM', '10 AM', '11 AM','1 PM', '3 PM'] } # Dictionary to DataFrame Exam_Schedule_df = pd.DataFrame(Exam_Schedule, index = ['Mon', 'Tue', 'Wed','Thu', 'Fri']) print(Exam_Schedule_df)
Output
Running the above code gives us the following result −
Exam Day Exam Subject Time 0 Mon Chemisry 2 PM 1 Tue Physics 10 AM 2 Wed Maths 10 AM 3 Thu English 2 PM 4 Fri Biology 10 AM
- Related Articles
- Create a Pandas Dataframe from a dict of equal length lists in Python
- Python - Convert a list of lists into tree-like dict
- Creating a Pandas dataframe column based on a given condition in Python
- Python - Column deletion from list of lists
- How to invert a matrix or nArray in Python?
- How do you create nested dict in Python?
- Get positive elements from given list of lists in Python
- Python Pandas - Create Multiindex from dataframe
- How to create a pandas DataFrame using a list of lists?
- In Python how to create dictionary from two lists?
- How to convert a dictionary to a matrix or nArray in Python?
- Python – Filter rows with only Alphabets from List of Lists
- Python – Strip whitespace from a Pandas DataFrame
- Accessing Values of Lists in Python
- Unpacking tuple of lists in Python
