Python数据分析与展示:pandas库的数据排序-12

简介: Python数据分析与展示:pandas库的数据排序-12

基本统计(含排序) 分布/累计统计 数据特征 相关性、周期性等 数据挖掘(形成知识)

一组数据表达一个或多个含义

摘要 - 数据形成有损特征的过程

pandas库的数据排序

.sort_index()方法在指定轴上根据索引进行排序,默认升序

.sort_index(axis=0, ascending=True)

.sort_values()方法在指定轴上根据数值进行排序,默认升序

Series.sort_values(axis=0, ascending=True)

DataFrame.sort_values(by, axis=0, ascending=True)

by :axis轴上的某个索引或索引列表


NaN统一放到排序末尾

代码示例

# -*- coding: utf-8 -*-
# @File    : pandas_sort.py
# @Date    : 2018-05-20
# pandas数据排序
import pandas as pd
import numpy as np
# 数据准备
df = pd.DataFrame(np.arange(20).reshape(4, 5), index=["c", "a", "d", "b"])
print(df)
"""
    0   1   2   3   4
c   0   1   2   3   4
a   5   6   7   8   9
d  10  11  12  13  14
b  15  16  17  18  19
"""
# 索引升序排序,默认axis=0,行索引
print(df.sort_index())
"""
    0   1   2   3   4
a   5   6   7   8   9
b  15  16  17  18  19
c   0   1   2   3   4
d  10  11  12  13  14
"""
# 索引降序排序
print(df.sort_index(ascending=False))
"""
    0   1   2   3   4
d  10  11  12  13  14
c   0   1   2   3   4
b  15  16  17  18  19
a   5   6   7   8   9
"""
# 对axis-1排序,列索引
print(df.sort_index(axis=1, ascending=False))
"""
    4   3   2   1   0
c   4   3   2   1   0
a   9   8   7   6   5
d  14  13  12  11  10
b  19  18  17  16  15
"""
# 值排序,行排序
print(df.sort_values(2, ascending=False))
"""
    0   1   2   3   4
b  15  16  17  18  19
d  10  11  12  13  14
a   5   6   7   8   9
c   0   1   2   3   4
"""
# 列排序,选择排序关键字
print(df.sort_values("a", axis=1, ascending=False))
"""
    4   3   2   1   0
c   4   3   2   1   0
a   9   8   7   6   5
d  14  13  12  11  10
b  19  18  17  16  15
"""
# NaN统一放到排序末尾
a = pd.DataFrame(np.arange(12).reshape(3, 4), index=["a", "b", "c"])
b = pd.DataFrame(np.arange(20).reshape(4, 5), index=["a", "b", "c", "d"])
c = a + b
print(c)
"""
      0     1     2     3   4
a   0.0   2.0   4.0   6.0 NaN
b   9.0  11.0  13.0  15.0 NaN
c  18.0  20.0  22.0  24.0 NaN
d   NaN   NaN   NaN   NaN NaN
"""
print(c.sort_values(2, ascending=False))
"""
      0     1     2     3   4
c  18.0  20.0  22.0  24.0 NaN
b   9.0  11.0  13.0  15.0 NaN
a   0.0   2.0   4.0   6.0 NaN
d   NaN   NaN   NaN   NaN NaN
"""
print(c.sort_values(2, ascending=True))
"""
      0     1     2     3   4
a   0.0   2.0   4.0   6.0 NaN
b   9.0  11.0  13.0  15.0 NaN
c  18.0  20.0  22.0  24.0 NaN
d   NaN   NaN   NaN   NaN NaN
"""


相关文章
|
4天前
|
数据处理 Python
如何使用Python的Pandas库进行数据排序和排名
【4月更文挑战第22天】Pandas Python库提供数据排序和排名功能。使用`sort_values()`按列进行升序或降序排序,如`df.sort_values(by='A', ascending=False)`。`rank()`函数用于计算排名,如`df['A'].rank(ascending=False)`。多列操作可传入列名列表,如`df.sort_values(by=['A', 'B'], ascending=[True, False])`和分别对'A'、'B'列排名。
14 2
|
1月前
|
数据处理 Python
如何使用Python的Pandas库进行数据排序和排名?
Pandas在Python中提供数据排序和排名功能。使用`sort_values()`进行排序,如`df.sort_values(by='A', ascending=False)`进行降序排序;用`rank()`进行排名,如`df['A'].rank(ascending=False)`进行降序排名。多列操作可传入列名列表,如`df.sort_values(by=['A', 'B'], ascending=[True, False])`。
25 6
|
1月前
|
索引 Python
如何使用Python的Pandas库进行数据合并和拼接?
【2月更文挑战第28天】【2月更文挑战第103篇】如何使用Python的Pandas库进行数据合并和拼接?
|
1月前
|
索引 Python
如何在Python中,Pandas库实现对数据的时间序列分析?
Pandas在Python中提供强大的时间序列分析功能,包括:1) 使用`pd.date_range()`创建时间序列;2) 通过`pd.DataFrame()`将时间序列转为DataFrame;3) `set_index()`设定时间列作为索引;4) `resample()`实现数据重采样(如按月、季度);5) `rolling()`进行移动窗口计算,如计算移动平均;6) 使用`seasonal_decompose()`进行季节性调整。这些工具适用于各种时间序列分析场景。
35 0
|
3月前
|
索引 Python
Python 教程之 Pandas(11)—— 索引和选择 series 的数据
Python 教程之 Pandas(11)—— 索引和选择 series 的数据
33 0
Python 教程之 Pandas(11)—— 索引和选择 series 的数据
|
3月前
|
索引 Python
Python 教程之 Pandas(4)—— 使用 Pandas 索引和选择数据
Python 教程之 Pandas(4)—— 使用 Pandas 索引和选择数据
62 1
Python 教程之 Pandas(4)—— 使用 Pandas 索引和选择数据
|
4月前
|
索引 Python
Python 教程之 Pandas(4)—— 使用 Pandas 索引和选择数据
Python 教程之 Pandas(4)—— 使用 Pandas 索引和选择数据
96 0
|
9月前
|
数据处理 Python
|
5月前
|
数据挖掘 Python
【Python】数据分析:结构化数分工具 Pandas | Series 与 DataFrame | 读取CSV文件数据
【Python】数据分析:结构化数分工具 Pandas | Series 与 DataFrame | 读取CSV文件数据
51 1
|
3天前
|
数据挖掘 数据处理 索引
如何使用Python的Pandas库进行数据筛选和过滤?
Pandas是Python数据分析的核心库,提供DataFrame数据结构。基本步骤包括导入库、创建DataFrame及进行数据筛选。示例代码展示了如何通过布尔索引、`query()`和`loc[]`方法筛选`Age`大于19的记录。
10 0