pandas排序列表str.split() [英] Pandas sort list of str.split()

查看:108
本文介绍了pandas排序列表str.split()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个类型为str的熊猫系列,我想对str.split返回的结果进行排序。



例如,给定系列

  s = pd.Series(['abc,def,ghi','ghi,abc'])

我想获得

  s2 = pd.Series(['abc,def,ghi','abc,ghi'])

as一个结果。



我该怎么做?我想到像 s.str.split(',')。sort()。但是,我在熊猫中找不到这样的排序功能。任何其他想法?



另一个想法是使用函数 get_dummies ,然后重新排列列,最后做某事像 join_dummies 。但是,我找不到这样的函数 join_dummies

解决方案

尝试这个:

 在[70]中:s.str.split(',')。map(lambda x:', .join(sorted(x)))
输出[70]:
0 abc,def,ghi
1 abc,ghi
dtype:object


Given a Pandas Series of type str, I want to sort the result returned by str.split.

For example, given the Series

s = pd.Series(['abc,def,ghi','ghi,abc'])

I would like to get

s2 = pd.Series(['abc,def,ghi','abc,ghi'])

as a result.

How can I do this? I thought about something like s.str.split(',').sort(). However, I could find no such sort function in Pandas. Any other ideas?

Another idea would be to use the function get_dummies, then rearange the columns and finally do something like join_dummies. However, I could not find such a function join_dummies.

解决方案

try this:

In [70]: s.str.split(',').map(lambda x: ','.join(sorted(x)))
Out[70]:
0    abc,def,ghi
1        abc,ghi
dtype: object

这篇关于pandas排序列表str.split()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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