按一列的子字符串对Pandas

按一列的子字符串对Pandas

本文介绍了按一列的子字符串对Pandas Dataframe进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个数据框:

    name             email
0   Carl    [email protected]
1    Bob     [email protected]
2  Alice   [email protected]
3  David  [email protected]
4    Eve     [email protected]

如何根据电子邮件的域名(按字母顺序,升序)对邮件进行排序,然后在每个域组中根据"@"之前的字符串对邮件进行排序?

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     [email protected]
1    Eve     [email protected]
2  David  [email protected]
3  Alice   [email protected]
4   Carl    [email protected]

推荐答案

选项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     [email protected]    Bob
1     [email protected]    Eve
2  [email protected]  David
3   [email protected]  Alice
4    [email protected]   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     [email protected]
1    Eve     [email protected]
2  David  [email protected]
3  Alice   [email protected]
4   Carl    [email protected]

这篇关于按一列的子字符串对Pandas Dataframe进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:00