- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Python Pandas - Create a DataFrame from original index but enforce a new index
To create a DataFrame from original index but enforce a new index, use the index.to_frame(). Set the parameter index to False.
At first, import the required libraries −
import pandas as pd
Creating Pandas index −
index = pd.Index(['Electronics','Accessories','Decor', 'Books', 'Toys'],name ='Products')
Display the Pandas index
print("Pandas Index...\n",index)
Enforce new index and convert index to DataFrame. Here, the actual index gets replaced by another index −
print("\nIndex to DataFrame...\n",index.to_frame(index=False))
Example
Following is the code −
import pandas as pd # Creating Pandas index index = pd.Index(['Electronics','Accessories','Decor', 'Books', 'Toys'],name ='Products') # Display the Pandas index print("Pandas Index...\n",index) # Return the number of elements in the Index print("\nNumber of elements in the index...\n",index.size) # Return the dtype of the data print("\nThe dtype object...\n",index.dtype) # Enforce new index and convert index to DataFrame # Here, the actual index gets replaced by another index print("\nIndex to DataFrame...\n",index.to_frame(index=False))
Output
This will produce the following output −
Pandas Index... Index(['Electronics', 'Accessories', 'Decor', 'Books', 'Toys'], dtype='object', name='Products') Number of elements in the index... 5 The dtype object... object Index to DataFrame... Products 0 Electronics 1 Accessories 2 Decor 3 Books 4 Toys
- Related Articles
- Python Pandas - Create a DataFrame from the TimeDeltaIndex object ignoring the original index
- Python Pandas - Create a DataFrame with both the original index and name
- Python Pandas - Create a DataFrame from DateTimeIndex ignoring the index
- Python - Create a new view of the Pandas Index
- Python Pandas - Create a Series with both the original index and name
- Python Pandas - Return a new Index with elements of index not in other but unsort the result
- Create a DataFrame with customized index parameters in Pandas
- Python Pandas - Insert a new index value at the first index from the last
- Python Pandas - Create a DataFrame with the levels of the MultiIndex as columns but avoid setting the index of the returned DataFrame
- Python – Drop a level from a multi-level column index in Pandas dataframe
- Python – Create a new column in a Pandas dataframe
- Python - Make new Pandas Index with deleting multiple index elements
- Python – Drop multiple levels from a multi-level column index in Pandas dataframe
- Python Pandas - Insert a new index value at a specific position
- Python Pandas - Display the index of dataframe in the form of multi-index

Advertisements