Reindexing means changing the index of the dataframe. Before learning about the reindexing let us first learn what is the difference between indexes and indices.
It is the plural of the index that is it shows the multiple indexes of the same dataframe. It also means many index labels within the index data structure.
It is also the plural of the index but showing multiple indexes of multiple (or all) dataframes or Series (can be from the same dataframe). It basically means many pandas index data structure.
Importing Weather data
An index is a privileged column in the dataframe providing access to dataframe rows.
In this example, we have taken the date as the index column and printed the max and min temp of the first five dates.
Here as we can see the index of the dataframe is the date and type of the index is the pandas Index.
Here we have used reindex function which takes a list as an argument and change the index of the dataframe to the values in the list. The value passed to the reindex function should be equal to the total number of values in your dataframe.
Here as we can see the sort index has sorted the index values in the numerical order if we have used a string as an index then it will sort the values of the index in alphabetical order.
Reindex from a Dataframe Index
The input argument to a dataframe reindex method can also be another dataframe’s index. But it can only be done if the second dataframe has a suitable index just like the first dataframe.
Reindexing with missing labels
If we specify an index that does not have value in original dataframe to reindex() method, a new row is inserted with a value NaN.
As we know dataset had no value for any of the new indexes so every value is NaN.
We can also check if the df dataframe has all the index of df1 dataframe.
As the df dataframe has all the dates as the index and no index of 1,2,3,4 or 5 so when we applied the index of df1 dataframe on df dataframe all the entries get NaN.
Sorting Dataframes with Index and Columns
Here we have changed the sorting order for the df2 dataframe and for df3 dataframe we have sorted according to value in Max TemperatureF column