python series 排序_Python:Pandas的DataFrame如何按指定list排序

本文详细介绍了如何使用Pandas的DataFrame按指定list进行排序,包括Series转换为DataFrame,设置数据类型为'category',使用reorder_categories()和set_categories()方法处理不同情况的排序问题。
摘要由CSDN通过智能技术生成

fde9467c0f92583b21229859ba440220.png

前言

写这篇文章的起由是有一天微信上一位朋友问到一个问题,问题大体意思概述如下:现在有一个pandas的Series和一个python的list,想让Series按指定的list进行排序,如何实现?

这个问题的需求用流程图描述如下:

ad1d4575e08d519890e38eae82b66cf8.png

我思考了一下,这个问题解决的核心是引入pandas的数据类型“category”,从而进行排序。

在具体的分析过程中,先将pandas的Series转换成为DataFrame,然后设置数据类型,再进行排序。思路用流程图表示如下:

2adb623e9c087892c1b19b596906ba7e.png

分析过程引入pandas库import pandas as pd构造Series数据s = pd.Series({'a':1,'b':2,'c':3})

sa    1

b    2

c    3

dtype: int64s.indexIndex(['a', 'b', 'c'], dtype='object')指定的list,后续按指定list的元素顺序进行排序list_custom = ['b', 'a', 'c']

list_custom['b', 'a', 'c']将Series转换成DataFramedf = pd.DataFrame(s)

df = df.reset_index()

df.columns = ['words', 'number']

dfwordsnumber0a1

1b2

2c3

设置成“category”数据类型# 设置成“category”数据类型

df['words'] = df['words'].astype('category')# inplace = True,使 recorder_categories生效

df['words'].cat.reorder_categories(list_custom, inplace=True)

# inplace = True,使 df生效

df.sort_values('words', inplace=True)

dfwordsnumber1b2

0a1

2c3

指定list元素多的情况:

若指定的list所包含元素比Dataframe中需要排序的列的元素多,怎么办?reorder_catgories()方法不能继续使用,因为该方法使用时要求新的categories和dataframe中的categories的元素个数和内容必须一致,只是顺序不同。

这种情况下,可以使用 set_categories()方法来实现。新的list可以比dataframe中元素多。list_custom_new = ['d', 'c', 'b','a','e']

dict_new = {'e':1, 'b':2, 'c':3}

df_new = pd.DataFrame(list(dict_new.items()), columns=['words', 'value'])

print(list_custom_new)

df_new.sort_values('words', inplace=True)

df_new['d', 'c', 'b', 'a', 'e']wordsvalue0b2

1c3

2e1df_new['words'] = df_new['words'].astype('category')

# inplace = True,使 set_categories生效

df_new['words'].cat.set_categories(list_custom_new, inplace=True)

df_new.sort_values('words', ascending=True)wordsvalue1c3

0b2

2e1

指定list元素少的情况:

若指定的list所包含元素比Dataframe中需要排序的列的元素少,怎么办?这种情况下,set_categories()方法还是可以使用的,只是没有的元素会以NaN表示

注意下面的list中没有元素“b”list_custom_new = ['d', 'c','a','e']

dict_new = {'e':1, 'b':2, 'c':3}

df_new = pd.DataFrame(list(dict_new.items()), columns=['words', 'value'])

print(list_custom_new)

df_new.sort_values('words', inplace=True)

df_new['d', 'c', 'a', 'e']wordsvalue0b2

1c3

2e1df_new['words'] = df_new['words'].astype('category')

# inplace = True,使 set_categories生效

df_new['words'].cat.set_categories(list_custom_new, inplace=True)

df_new.sort_values('words', ascending=True)wordsvalue0NaN2

1c3

2e1

总结

根据指定的list所包含元素比Dataframe中需要排序的列的元素的多或少,可以分为三种情况:相等的情况下,可以使用 reorder_categories和 set_categories方法;

list的元素比较多的情况下, 可以使用set_categories方法;

list的元素比较少的情况下, 也可以使用set_categories方法,但list中没有的元素会在DataFrame中以NaN表示。

源代码

需要的童鞋可在微信公众号“Python数据之道”(ID:PyDataRoad)后台回复关键字获取视频,关键字如下:

“2017025”(不含引号)

更多精彩文章

请点击微信公众号底部菜单栏。

文章集锦:

Pandas

可视化

项目实战

其他

ac243d2244a13e0bd4d2c9c7f11175a4.png

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值