如何反转熊猫数据框中的字符串?

时间:2019-10-21 21:38:01

标签: 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
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'

我该如何解决这个问题?

5 个答案:

答案 0 :(得分:3)

  

这给我一个错误,

这是因为str.contains(", ")返回一个布尔序列,并且没有方法split

无论如何,请尝试

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

答案 1 :(得分:3)

要修复代码np.where

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

答案 2 :(得分:3)

使用Series.where

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

   ID           col1
0   1   Michael Owen
1   2  Stephen Curry
2   3  Lionel, Messi
3   4  LeBron, James

如果要放','

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

   ID           col1
0   1   Michael Owen
1   2  Stephen Curry
2   3  Lionel  Messi
3   4  LeBron  James

答案 3 :(得分:2)

您只需要在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]))

答案 4 :(得分:0)

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

  1. apply完成工作时,无需使用str[::-1]来翻转列表
  2. 在逗号apply上分割时,无需使用','或任何其他技巧,将以任何一种方式生成一个列表,并且将一个元素列表反转为相同的列表。这意味着我的方法可以安全地使用没有逗号的名称。我不需要if / then构造。
  3. 我用两种不同的方式处理潜在的空间。
    1. 使用正则表达式
    2. 使用str.strip

正则表达式和pandas.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'] = [' '.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