...
/Solution: Sorting, Columns, Filtering, and Indexing Operations
Solution: Sorting, Columns, Filtering, and Indexing Operations
See the solution to problems related to sorting, columns, filtering and indexing operations.
We'll cover the following...
Solution 1
Given the following dataframe:
col1 col2 col3 col40 A B C D1 E F NaN H2 I J Z Y3 S X E V4 Q A B W
Perform the following operations:
a) Sort the index.
b) Sort by a single column col1
.
c) Sort by col1
in descending order.
d) Sort by two columns, col1
and col2
.
Sort the index
Press + to interact
import pandas as pdimport numpy as npdf=pd.DataFrame({"": ['A','B','C','D'],"col1": ['E','F',np.NaN, 'H'],"col2": ['I','J','Z','Y'],"col3": ['S','X','E','V'],"col4": ['Q', 'A','B','W']})print(df.sort_index(axis='columns'))
Line 12: We sort the DataFrame
df
by the column indices. It rearranges the columns in the DataFrame in alphabetical order.
...