在列中移动文本

2024-06-16 09:47:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含搜索结果的数据框。是否可以在列中移动文本,使名称始终位于第一位

Results
'Phil Spencer', 'Microsoft'
'Larry Hryb', 'Microsoft'
'Microsoft', 'Bill Gates'
'Sony', 'Kenichiro, Yoshida'
'Sony', 'PS5', 'Howard Stringer'

期望

Results
'Phil Spencer', 'Microsoft'
'Larry Hryb', 'Microsoft'
'Bill Gates', 'Microsoft'
'Kenichiro, Yoshida','Sony'
'Howard Stringer', 'Sony', 'PS5'

我想要它,以便名称始终位于列的第一位。有没有办法做到这一点


Tags: 名称resultsmicrosoftspencerbilllarrysonygates
1条回答
网友
1楼 · 发布于 2024-06-16 09:47:48

这是一个相当困难的问题,但我们可以假设任何带有space的东西都是一个名称,并尝试以这种方式排序

首先让我们用,分割,它只在'之后进行,后面是一个空格\s


s = df['Results'].str.split("',\s",expand=True).stack()

0  0           'Phil Spencer
   1             'Microsoft'
1  0             'Larry Hryb
   1             'Microsoft'
2  0              'Microsoft
   1            'Bill Gates'
3  0                   'Sony
   1    'Kenichiro, Yoshida'
4  0                   'Sony
   1                    'PS5
   2       'Howard Stringer'


new_results = (
    s.loc[s.str.contains("\s{1}").astype(int).sort_values(ascending=False).index]
    .replace({"'": "", ",": ""}, regex=True)
    .groupby(level=[0])
    .agg(", ".join)
)

0       Phil Spencer, Microsoft
1         Larry Hryb, Microsoft
2         Bill Gates, Microsoft
3       Kenichiro Yoshida, Sony
4    Howard Stringer, PS5, Sony

另一个计算成本更高的解决方案是按每个对象的len进行排序,但您可以看到这并不是万无一失的,因为有些公司的名称可能比名字长

(
    s.loc[s.apply(len).sort_values(ascending=False).index]
     .replace({"'": "", ",": ""}, regex=True)
    .groupby(level=[0])
    .agg(", ".join)
)

0       Phil Spencer, Microsoft
1         Microsoft, Larry Hryb # <  wrong. 
2         Bill Gates, Microsoft
3       Kenichiro Yoshida, Sony
4    Howard Stringer, Sony, PS5
dtype: object

相关问题 更多 >