按一列的子字符串对Pandas Dataframe进行排序 [英] Sort Pandas Dataframe by substrings of a column
本文介绍了按一列的子字符串对Pandas Dataframe进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出一个数据框:
name email
0 Carl carl@yahoo.com
1 Bob bob@gmail.com
2 Alice alice@yahoo.com
3 David dave@hotmail.com
4 Eve eve@gmail.com
如何根据电子邮件的域名(按字母顺序,升序)对邮件进行排序,然后在每个域组中根据"@"之前的字符串对邮件进行排序?
How can it be sorted according to the email's domain name (alphabetically, ascending), and then, within each domain group, according to the string before the "@"?
对以上内容进行排序的结果应为:
The result of sorting the above should then be:
name email
0 Bob bob@gmail.com
1 Eve eve@gmail.com
2 David dave@hotmail.com
3 Alice alice@yahoo.com
4 Carl carl@yahoo.com
推荐答案
选项1
sorted
+ reindex
Option 1
sorted
+ reindex
df = df.set_index('email')
df.reindex(sorted(df.index, key=lambda x: x.split('@')[::-1])).reset_index()
email name
0 bob@gmail.com Bob
1 eve@gmail.com Eve
2 dave@hotmail.com David
3 alice@yahoo.com Alice
4 carl@yahoo.com Carl
选项2
sorted
+ pd.DataFrame
或者,您可以通过重新创建新的DataFrame来放弃选项1中的reindex
调用.
Option 2
sorted
+ pd.DataFrame
As an alternative, you can ditch the reindex
call from Option 1 by re-creating a new DataFrame.
pd.DataFrame(
sorted(df.values, key=lambda x: x[1].split('@')[::-1]),
columns=df.columns
)
name email
0 Bob bob@gmail.com
1 Eve eve@gmail.com
2 David dave@hotmail.com
3 Alice alice@yahoo.com
4 Carl carl@yahoo.com
这篇关于按一列的子字符串对Pandas Dataframe进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文