如何使用python pandas根据特定的(字符串)列对数据框进行排序? [英] how to sort dataframe based on particular (string)columns using python pandas?

查看:1005
本文介绍了如何使用python pandas根据特定的(字符串)列对数据框进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的熊猫数据框包含以下数据:

My Pandas data frame contains the following data:

product,values
 a1,     10
 a5,     20
 a10,    15
 a2,     45
 a3,     12
 a6,     67

我必须根据产品列对该数据框进行排序.因此,我想得到以下输出:

I have to sort this data frame based on the product column. Thus, I would like to get the following output:

product,values
 a10,     15
 a6,      67
 a5,      20
 a3,      12
 a2,      45
 a1,      10

不幸的是,我遇到以下错误:

Unfortunately, I'm facing the following error:

ErrorDuringImport(path,sys.exc_info())

ErrorDuringImport(path, sys.exc_info())

ErrorDuringImport:视图中的问题-键入'exceptions.Indentation

ErrorDuringImport: problem in views - type 'exceptions.Indentation

推荐答案

您可以首先 sort_values >,最后一个 drop 此列:

You can first extract digits and cast to int by astype. Then sort_values of column sort and last drop this column:

df['sort'] = df['product'].str.extract('(\d+)', expand=False).astype(int)
df.sort_values('sort',inplace=True, ascending=False)
df = df.drop('sort', axis=1)
print (df)
  product  values
2     a10      15
5      a6      67
1      a5      20
4      a3      12
3      a2      45
0      a1      10

这是必要的,因为如果仅使用 sort_values :

It is necessary, because if use only sort_values:

df.sort_values('product',inplace=True, ascending=False)
print (df)
  product  values
5      a6      67
1      a5      20
4      a3      12
3      a2      45
2     a10      15
0      a1      10

另一个想法是使用 natsort 库:

Another idea is use natsort library:

from natsort import index_natsorted, order_by_index

df = df.reindex(index=order_by_index(df.index, index_natsorted(df['product'], reverse=True)))
print (df)
  product  values
2     a10      15
5      a6      67
1      a5      20
4      a3      12
3      a2      45
0      a1      10

这篇关于如何使用python pandas根据特定的(字符串)列对数据框进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆