Python 如何反转数据帧中的字符串?

Python 如何反转数据帧中的字符串?,python,string,pandas,dataframe,Python,String,Pandas,Dataframe,我的数据框就像 ID col1 1 Michael Owen 2 Stephen Curry 3 Messi, Lionel 4 James, LeBron 我试图颠倒那些被,“分割的名字的顺序 我的密码是 df['col1']=df.col1.str.split().apply(lambda x:','.join(x[::-1]) 但它会反转所有行,即使名称被“拆分 ID col1 1 Owen, Michael 2 Curry, Stephen

我的数据框就像

ID   col1
1    Michael Owen
2    Stephen Curry
3    Messi, Lionel
4    James, LeBron
我试图颠倒那些被
,“
分割的名字的顺序

我的密码是

df['col1']=df.col1.str.split().apply(lambda x:','.join(x[::-1])

但它会反转所有行,即使名称被
拆分

ID   col1
1    Owen, Michael
2    Curry, Stephen
3    Lionel, Messi
4    LeBron, James
然后我试着

df.loc[df['col1'].str.contains(“,”).split(“col1”)].apply(lambda x:“,”.join(x[::-1])

这给了我一个错误

AttributeError: 'Series' object has no attribute 'split'
我怎样才能解决这个问题

这给了我一个错误

AttributeError: 'Series' object has no attribute 'split'
这是因为
str.contains(“,”)
返回一个布尔序列,并且它没有方法
split

无论如何,试试看

df.col1.str.split(',').str[1] + ',' + df.col1.str.split(',').str[0]

修复代码
np.where

df['col1']=np.where(df.col1.str.contains(','),df.col1.str.split(', ').apply(lambda x: ', '.join(x[::-1])),df.col1)
使用:



如果要删除“
”,“

df['col1']=( df.col1.str.split()
              .apply(lambda x: ', '.join(x[::-1]).rstrip(','))
              .where(df['col1'].str.contains(','),df['col1']) 
              .str.replace(',','') )


只需在
split()
中加一个逗号,如下所示:

df['col1'] = df.col1.str.split(',').apply(lambda x: ', '.join(x[::-1]))
如果要反转并删除“,”,请将其从
join
方法中删除

df['col1'] = df.col1.str.split(',').apply(lambda x: ' '.join(x[::-1]))

我选择不使用逗号来表示最终值。如果需要逗号,请将
join
字符串从
'
更改为
,'
。我添加这些答案是因为:

  • str[::-1]
    执行作业时,无需使用
    apply
    来反转列表
  • 在逗号上拆分时,无需使用
    apply
    或任何其他技巧,“将以任何方式生成一个列表,而反转一个元素列表就是同一个列表。这意味着我的方法可以安全地在没有逗号的名称上运行。我不需要if/then构造
  • 我用两种不同的方式处理潜在空间。
  • 用正则表达式
  • str.strip

  • 正则表达式和熊猫.Series.str。。。 。。。被锁了3次

    #                          regex expression  
    #                          split on comma
    #                          followed by zero
    #                          or more spaces
    #                               /--\
    df['col1'] = df.col1.str.split(',\s*').str[::-1].str.join(' ')
    
    df
    
       ID           col1
    0   1   Michael Owen
    1   2  Stephen Curry
    2   3   Lionel Messi
    3   4   LeBron James
    

    理解和
    str.strip
    可能重复的
    df['col1'] = df.col1.str.split(',').apply(lambda x: ' '.join(x[::-1]))
    
    #                          regex expression  
    #                          split on comma
    #                          followed by zero
    #                          or more spaces
    #                               /--\
    df['col1'] = df.col1.str.split(',\s*').str[::-1].str.join(' ')
    
    df
    
       ID           col1
    0   1   Michael Owen
    1   2  Stephen Curry
    2   3   Lionel Messi
    3   4   LeBron James
    
    df['col1'] = [' '.join([*map(str.strip, x.split(','))][::-1]) for x in df.col1]
    
    df
    
       ID           col1
    0   1   Michael Owen
    1   2  Stephen Curry
    2   3   Lionel Messi
    3   4   LeBron James