Pandas Learning: Sorting series and Dataframe __pandas

Source: Internet
Author: User

This question mainly writes the method of sorting series and dataframe according to index or value

Code:

#coding =utf-8
Import pandas as PD
import numpy as NP
#以下实现排序功能.
SERIES=PD. Series ([3,4,1,6],index=[' B ', ' A ', ' d ', ' C '])
FRAME=PD. Dataframe ([[2,4,1,5],[3,1,4,5],[5,1,4,2]],columns=[' B ', ' A ', ' d ', ' C '],index=[' one ', ' two ', ' three '])
print
the frame print series
print ' series is sorted by index: '
print series.sort_index ()
print ' series sorted by value: '
Print series.sort_values ()
print ' dataframe sorted by row index in descending order (default ascending sort, adjust ascending parameters): '
print Frame.sort_index ( Ascending=false)
print ' dataframe sorted by column index: '
print Frame.sort_index (axis=1)
print ' Dataframe Sort by value: '
print frame.sort_values (by= ' a ')
print ' Sorted through multiple indexes: '
print frame.sort_values (by= [' A ', ' C '])

Experimental results:

b A D C
One 2 4 1 5
Two 3 1 4 5
Three 5 1 4 2
B 3
A 4
D 1
C 6
Dtype:int64
Series Sort by index:
A 4
B 3
C 6
D 1
Dtype:int64
Series are sorted by value:
D 1
B 3
A 4
C 6
Dtype:int64
Dataframe sorted by row index in descending order (default ascending sort, adjust ascending parameters):
b A D C
Two 3 1 4 5
Three 5 1 4 2
One 2 4 1 5
Dataframe sorts according to the column index:
A B c D
One 4 2 5 1
Two 1 3 5 4
Three 1 5 2 4
Dataframe Sort By Value:
b A D C
Two 3 1 4 5
Three 5 1 4 2
One 2 4 1 5
Sort by two indexes:
b A D C
Three 5 1 4 2
Two 3 1 4 5
One 2 4 1 5
[Finished in 1.0s]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.