Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

sorting by column in pandas

# Python, Pandas
# Sorting dataframe df on the values of a column col1

# Return sorted array without modifying the original one
df.sort_values(by=["col1"]) 

# Sort the original array permanently
df.sort_values(by=["col1"], inplace = True)
Comment

sorting rows and columns in pandas

df.sort_values(by="ColumnName", axis=0, ascending=False, inplace=False, kind='quicksort')
#axis 0 is rows and axis 1 is columns. For axis 0 by needs to contain column name  
Comment

Sorting Dataframes by Column Python Pandas

# Sorting Pandas Dataframe in Descending Order
  
# importing pandas library
import pandas as pd
  
# Initializing the nested list with Data set
age_list = [['Afghanistan', 1952, 8425333, 'Asia'],
            ['Australia', 1957, 9712569, 'Oceania'],
            ['Brazil', 1962, 76039390, 'Americas'],
            ['China', 1957, 637408000, 'Asia'],
            ['France', 1957, 44310863, 'Europe'],
            ['India', 1952, 3.72e+08, 'Asia'],
            ['United States', 1957, 171984000, 'Americas']]
  
# creating a pandas dataframe
df = pd.DataFrame(age_list, columns=['Country', 'Year',
                                     'Population', 'Continent'])
  
# Sorting by column "Population"
df.sort_values(by=['Population'], ascending=False)
Comment

PREVIOUS NEXT
Code Example
Python :: import scipy python 
Python :: webbrowser python could not locate runnable browser 
Python :: input spaces seperated integers in python 
Python :: python initialize multidimensional list 
Python :: pandas uniqe values in the columns 
Python :: python get current time in seconds 
Python :: python error get line 
Python :: how to set learning rate in keras 
Python :: python remove cached package 
Python :: horizontal line for pyplot 
Python :: cv2 draw box 
Python :: python virtual environment 
Python :: python check if is pandas dataframe 
Python :: python pyodbc install 
Python :: jupyter print full dataframe 
Python :: set cuda visible devices python 
Python :: pandas to csv without header 
Python :: get image height width cv2 
Python :: pyyaml install 
Python :: matplotlib add space between subplots 
Python :: filter by row contains pandas 
Python :: python get ip from hostname 
Python :: python install required packages 
Python :: how to save plot in python 
Python :: python iterate dictionary key value 
Python :: install wxpython 
Python :: qtimer python 
Python :: how to scroll by in selenium python 
Python :: openpyxl read excel 
Python :: python server http one line 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =