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

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

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

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.