In [1]:
import pandas as pd
In [2]:
person = {
    "first": ["Kushagra", "Jane", "John","Adam","Eve"],
    "last": ["Gupta", "Doe", "Doe","Doe","Damm"],
    "email": ["Kushagra225@gmail.com", "JaneDoe123@gmail.com", "John@gmail.com","Adam@gmail.com","Eve@gmail.com"],
    "salary":[72000,60000,50000,100000,80000]
}
In [3]:
df = pd.DataFrame(person)
In [4]:
df
Out[4]:
first last email salary
0 Kushagra Gupta Kushagra225@gmail.com 72000
1 Jane Doe JaneDoe123@gmail.com 60000
2 John Doe John@gmail.com 50000
3 Adam Doe Adam@gmail.com 100000
4 Eve Damm Eve@gmail.com 80000
In [5]:
#sorting ascending
df.sort_values(by='last')
Out[5]:
first last email salary
4 Eve Damm Eve@gmail.com 80000
1 Jane Doe JaneDoe123@gmail.com 60000
2 John Doe John@gmail.com 50000
3 Adam Doe Adam@gmail.com 100000
0 Kushagra Gupta Kushagra225@gmail.com 72000
In [6]:
#sorting descending
df.sort_values(by='last', ascending=False)
Out[6]:
first last email salary
0 Kushagra Gupta Kushagra225@gmail.com 72000
1 Jane Doe JaneDoe123@gmail.com 60000
2 John Doe John@gmail.com 50000
3 Adam Doe Adam@gmail.com 100000
4 Eve Damm Eve@gmail.com 80000
In [7]:
#sorting by multiple columns
df.sort_values(by=['last','first'], ascending=False)
Out[7]:
first last email salary
0 Kushagra Gupta Kushagra225@gmail.com 72000
2 John Doe John@gmail.com 50000
1 Jane Doe JaneDoe123@gmail.com 60000
3 Adam Doe Adam@gmail.com 100000
4 Eve Damm Eve@gmail.com 80000
In [8]:
#sorting last name with ascending and first name with descending order
df.sort_values(by=['last','first'], ascending=[True,False])
Out[8]:
first last email salary
4 Eve Damm Eve@gmail.com 80000
2 John Doe John@gmail.com 50000
1 Jane Doe JaneDoe123@gmail.com 60000
3 Adam Doe Adam@gmail.com 100000
0 Kushagra Gupta Kushagra225@gmail.com 72000
In [9]:
#saving the changes in original df
df.sort_values(by=['last','first'], ascending=[True,False], inplace=True)
In [10]:
df
Out[10]:
first last email salary
4 Eve Damm Eve@gmail.com 80000
2 John Doe John@gmail.com 50000
1 Jane Doe JaneDoe123@gmail.com 60000
3 Adam Doe Adam@gmail.com 100000
0 Kushagra Gupta Kushagra225@gmail.com 72000
In [12]:
#sorting based on index using sort_index method
df = df.sort_index()
In [13]:
df
Out[13]:
first last email salary
0 Kushagra Gupta Kushagra225@gmail.com 72000
1 Jane Doe JaneDoe123@gmail.com 60000
2 John Doe John@gmail.com 50000
3 Adam Doe Adam@gmail.com 100000
4 Eve Damm Eve@gmail.com 80000
In [14]:
#sorting series objects
df['last'].sort_values()
Out[14]:
4     Damm
1      Doe
2      Doe
3      Doe
0    Gupta
Name: last, dtype: object
In [15]:
#top 3 salary
df['salary'].nlargest(3)
Out[15]:
3    100000
4     80000
0     72000
Name: salary, dtype: int64
In [16]:
#top 3 salary with details
df.nlargest(3,'salary')
Out[16]:
first last email salary
3 Adam Doe Adam@gmail.com 100000
4 Eve Damm Eve@gmail.com 80000
0 Kushagra Gupta Kushagra225@gmail.com 72000
In [17]:
#bottom 3 salary with details
df.nsmallest(3,'salary')
Out[17]:
first last email salary
2 John Doe John@gmail.com 50000
1 Jane Doe JaneDoe123@gmail.com 60000
0 Kushagra Gupta Kushagra225@gmail.com 72000