How to reindex Pandas Series and DataFrames?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to reindex Pandas Series and DataFrames?

How to reindex Pandas Series and DataFrames?

This recipe helps you reindex Pandas Series and DataFrames

5

Recipe Objective

While working on a dataset we sometimes needs to change the index of rows or column. So making a new dataframe for this may not be possible.

This python source code does the following:
1. Creates your own data dictionary and converts it into dataframe
2. Imputes the missing values
3. Re-indexes your dataframe

So this is the recipe on we can reindex Pandas Series and DataFrame

Step 1 - Import the library

import pandas as pd

We have only imported pandas which is needed.

Step 2 - Setting up the Data

We have created a dictionary of data and passed it in pd.DataFrame to make a dataframe with columns 'first_name', 'last_name', 'age', 'Comedy_Score' and 'Rating_Score'. raw_data = {'first_name': ['Sheldon', 'Raj', 'Leonard', 'Howard', 'Amy'], 'last_name': ['Copper', 'Koothrappali', 'Hofstadter', 'Wolowitz', 'Fowler'], 'age': [42, 38, 36, 41, 35], 'Comedy_Score': [9, 7, 8, 8, 5], 'Rating_Score': [25, 25, 49, 62, 70]} df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'Comedy_Score', 'Rating_Score']) print(df)

Step 3 - Reindexing the DataFrame

Here we will be reindexing both rows and column. First we have used df.reindex function to change the index of rows and then we have defined an object to set the list of column names for reindexing and we have passed that list into the function df.reindex to change the index of columns. print(df.reindex([4, 3, 2, 1, 0])) col_Title = ['last_name', 'first_name', 'Comedy_Score' , 'Rating_Score', 'age'] print(df.reindex(columns=col_Title)) So the output comes as

  first_name     last_name  age  Comedy_Score  Rating_Score
0    Sheldon        Copper   42             9            25
1        Raj  Koothrappali   38             7            25
2    Leonard    Hofstadter   36             8            49
3     Howard      Wolowitz   41             8            62
4        Amy        Fowler   35             5            70

  first_name     last_name  age  Comedy_Score  Rating_Score
4        Amy        Fowler   35             5            70
3     Howard      Wolowitz   41             8            62
2    Leonard    Hofstadter   36             8            49
1        Raj  Koothrappali   38             7            25
0    Sheldon        Copper   42             9            25

      last_name first_name  Comedy_Score  Rating_Score  age
0        Copper    Sheldon             9            25   42
1  Koothrappali        Raj             7            25   38
2    Hofstadter    Leonard             8            49   36
3      Wolowitz     Howard             8            62   41
4        Fowler        Amy             5            70   35

Relevant Projects

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.