Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

df sort values

>>> df.sort_values(by=['col1'], ascending = False)
    col1 col2 col3
0   A    2    0
1   A    1    1
2   B    9    9
5   C    4    3
4   D    7    2
3   NaN  8    4
Comment

sort a dataframe by a column valuepython

>>> df.sort_values(by=['col1'])
    col1 col2 col3
0   A    2    0
1   A    1    1
2   B    9    9
5   C    4    3
4   D    7    2
3   NaN  8    4
Comment

dataframe sort by column

sorted = df.sort_values('column-to-sort-on', ascending=False)
#or
df.sort_values('name', inplace=True) 
Comment

dataframe, sort by columns

final_df = df.sort_values(by=['2'], ascending=False)
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

pandas sort dataframe by column

# Basic syntax:
import pandas as pd
df.sort_values(by=['col1'])

# Note, this does not sort in place unless you add inplace=True
# Note, add ascending=False if you want to sort in decreasing order
# Note, to sort by more than one column, add other column names to the
#	list like by=['col1', 'col2']
Comment

pandas dataframe sort by column

df.sort_values(by=['col1])
Comment

PREVIOUS NEXT
Code Example
Python :: plotly grid lines color 
Python :: find root directory of jupyter notebook 
Python :: opencv grayscale to rgb 
Python :: count how many duplicates python pandas 
Python :: openpyxl font 
Python :: check pip version 
Python :: tqdm in for loop 
Python :: pandas datetime now 
Python :: panda dataframe to list 
Python :: get current month python 
Python :: how to sum digits of a number in python 
Python :: how to get user location in python 
Python :: cv2 hconcat 
Python :: remove single and double quotes from string python 
Python :: run unittest in terminal python 
Python :: sklearn mean square error 
Python :: matplotlib matrix plot 
Python :: convert 1 digit to 2 digit python 
Python :: get the number of today week python 
Python :: cv2 show image 
Python :: wtf forms required 
Python :: how to minimize command console python 
Python :: python remove text between parentheses 
Python :: how to make text bold in tkinter 
Python :: how to get the current web page link in selenium pthon 
Python :: pandas ttable with sum totals 
Python :: chech box in tkinter 
Python :: generate openai schema 
Python :: display text in pygame 
Python :: python save list to text 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =