第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

Python:pandas的DataFrame如何按指定list排序

標(biāo)簽:
Python

前言

写这篇文章的起由是有一天微信上一位朋友问到一个问题,问题大体意思概述如下:

现在有一个pandas的Series和一个python的list,想让Series按指定的list进行排序,如何实现?

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

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

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

分析过程

  • 引入pandas库

import pandas as pd
  • 构造Series数据

s = pd.Series({'a':1,'b':2,'c':3})
s
a    1
b    2
c    3
dtype: int64
s.index
Index(['a', 'b', 'c'], dtype='object')
  • 指定的list,后续按指定list的元素顺序进行排序

list_custom = ['b', 'a', 'c']
list_custom
['b', 'a', 'c']
  • 将Series转换成DataFrame

df = pd.DataFrame(s)
df = df.reset_index()
df.columns = ['words', 'number']
df

1.jpg

设置成“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)
df

2.jpg

指定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']

3.jpg

df_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)

4.jpg

指定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']

5.jpg

df_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)

6.jpg

总结

根据指定的list所包含元素比Dataframe中需要排序的列的元素的多或少,可以分为三种情况:

  • 相等的情况下,可以使用 reorder_categories和 set_categories方法;

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

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

源代码

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

2017-025”(不含引号)



作者:leenard
链接:https://www.jianshu.com/p/2d3dd3e30d51

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報(bào)

0/150
提交
取消