-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathda_8sorting.py
More file actions
34 lines (29 loc) 路 1.4 KB
/
Copy pathda_8sorting.py
File metadata and controls
34 lines (29 loc) 路 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Sorting
"""
sorting means arranging the content in ascending or descending order . Data of dataframe can
be sorted according to values of row and columns . By default , sorting is fone on row labels in
ascending order .
there are two kinds of sorting available in Pandas (dataframe)
--By values (column)
--By index
Pandas dataframe provide two useful sort functions:
1.sort_values : it sorts the data of given column to the function in ascending or descending order
2.sort_index : it sorts rows( axis=0) and columns (axis =1).
"""
import pandas
df=pandas.DataFrame({"name":["vijaya" , "rahul" , "meghna" , "radhika" , "shaurya"],
"english":[67,78,75.5,88.5,92], "math":[78,67,75,88,92], "ip":[78,88,98,90,56]} ,
index =['A','B','C','D','E'])
print(df)
print("Sorting based on english marks :\n" , df.sort_values('english'))
print("Sorting based on math marks :\n" , df.sort_values('math'))
"""
Parameters
by: Specifies the column to sort by.
ascending: A boolean (True for ascending, False for descending).
inplace: If True, the original DataFrame is modified otherwise a new sorted DataFrame is returned.
na_position: Controls where NaN values are placed. Use 'first' to put NaNs at the top or 'last' (default) to place them at the end.
ignore_index: If True, resets the index after sorting
"""
print("sorting based on index row \n" , df.sort_index(axis =0))
print("sorting based on index column \n" , df.sort_index(axis =1))