Wednesday 8 November 2023

Sort the DataFrame by its index in Pandas

DataFrame index is an unique identifier assigned to each row.

For example,

data = {'Name': ['Krishna', 'Sailu', 'Joel', 'Chamu', 'Jitendra', "Raj"],
        'Age': [34, 35, 29, 35, 52, 34],
        'City': ['Bangalore', 'Hyderabad', 'Hyderabad', 'Chennai', 'Bangalore', 'Chennai'],
        'Gender': ['Male', 'Female', 'Male', 'Female', 'Male', 'Male'],
        'Grade': ['A', 'B', 'C', 'A', 'B', 'C']}
df = pd.DataFrame(data)

Above snippet generate below DataFrame.

       Name  Age       City  Gender Grade
0   Krishna   34  Bangalore    Male     A
1     Sailu   35  Hyderabad  Female     B
2      Joel   29  Hyderabad    Male     C
3     Chamu   35    Chennai  Female     A
4  Jitendra   52  Bangalore    Male     B
5       Raj   34    Chennai    Male     C

Default indexing starts with the number 0 and incremented by 1 for the subsequent rows. First row is assigned with the number 0, second row is with the number 1 etc.,

 

Following are the characteristics of DataFrame index.

a.   Immutable: Index is immutable, you can’t modify it once it is assigned to a DataFrame, but you can reassign a completely new index to the DataFrame.

b.   Unique: Index value within a DataFrame must be unique to make sure that the row is identified uniquely.

 

Sort the DataFrame by index

Using sort_index method, we can sort the DataFrame by index.

 

For example, let’s try with the below data set.

       Name  Age       City  Gender Grade
2      Joel   29  Hyderabad    Male     C
0   Krishna   34  Bangalore    Male     A
5       Raj   34    Chennai    Male     C
1     Sailu   35  Hyderabad  Female     B
3     Chamu   35    Chennai  Female     A
4  Jitendra   52  Bangalore    Male     B

Sort by index in ascending order

df = df.sort_index()

Above snippet generate below data set.

       Name  Age       City  Gender Grade
0   Krishna   34  Bangalore    Male     A
1     Sailu   35  Hyderabad  Female     B
2      Joel   29  Hyderabad    Male     C
3     Chamu   35    Chennai  Female     A
4  Jitendra   52  Bangalore    Male     B
5       Raj   34    Chennai    Male     C

Sort by index in descending order

df = df.sort_index(ascending=False)

Above snippet generate below data set.

       Name  Age       City  Gender Grade
5       Raj   34    Chennai    Male     C
4  Jitendra   52  Bangalore    Male     B
3     Chamu   35    Chennai  Female     A
2      Joel   29  Hyderabad    Male     C
1     Sailu   35  Hyderabad  Female     B
0   Krishna   34  Bangalore    Male     A

Find the below working application.

 

sort_by_index.py

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Sailu', 'Joel', 'Chamu', 'Jitendra', "Raj"],
        'Age': [34, 35, 29, 35, 52, 34],
        'City': ['Bangalore', 'Hyderabad', 'Hyderabad', 'Chennai', 'Bangalore', 'Chennai'],
        'Gender': ['Male', 'Female', 'Male', 'Female', 'Male', 'Male'],
        'Grade': ['A', 'B', 'C', 'A', 'B', 'C']}
df = pd.DataFrame(data)
print('Original DataFrame')
print(df)

print('\nSort the DataFrame by Age')
df = df.sort_values('Age')
print(df)

print('\nSort by index')
df = df.sort_index()
print(df)

print('\nSort by index in descending')
df = df.sort_index(ascending=False)
print(df)

Output

Original DataFrame
       Name  Age       City  Gender Grade
0   Krishna   34  Bangalore    Male     A
1     Sailu   35  Hyderabad  Female     B
2      Joel   29  Hyderabad    Male     C
3     Chamu   35    Chennai  Female     A
4  Jitendra   52  Bangalore    Male     B
5       Raj   34    Chennai    Male     C

Sort the DataFrame by Age
       Name  Age       City  Gender Grade
2      Joel   29  Hyderabad    Male     C
0   Krishna   34  Bangalore    Male     A
5       Raj   34    Chennai    Male     C
1     Sailu   35  Hyderabad  Female     B
3     Chamu   35    Chennai  Female     A
4  Jitendra   52  Bangalore    Male     B

Sort by index
       Name  Age       City  Gender Grade
0   Krishna   34  Bangalore    Male     A
1     Sailu   35  Hyderabad  Female     B
2      Joel   29  Hyderabad    Male     C
3     Chamu   35    Chennai  Female     A
4  Jitendra   52  Bangalore    Male     B
5       Raj   34    Chennai    Male     C

Sort by index in descending
       Name  Age       City  Gender Grade
5       Raj   34    Chennai    Male     C
4  Jitendra   52  Bangalore    Male     B
3     Chamu   35    Chennai  Female     A
2      Joel   29  Hyderabad    Male     C
1     Sailu   35  Hyderabad  Female     B
0   Krishna   34  Bangalore    Male     A



Previous                                                 Next                                                 Home

No comments:

Post a Comment