【问题标题】:How to reverse the order of first and last name in a Pandas Series如何颠倒熊猫系列中名字和姓氏的顺序
【发布时间】:2017-02-12 15:32:32
【问题描述】:

我有一个熊猫系列:

names = pd.Series([
'Andre Agassi',
'Barry Bonds',
'Christopher Columbus',
'Daniel Defoe',
'Emilio Estevez',
'Fred Flintstone',
'Greta Garbo',
'Humbert Humbert',
'Ivan Ilych'])

看起来像这样:

0            Andre Agassi
1             Barry Bonds
2    Christopher Columbus
3            Daniel Defoe
4          Emilio Estevez
5         Fred Flintstone
6             Greta Garbo
7         Humbert Humbert
8              Ivan Ilych

我想把它变成这样:

0            Agassi, Andre
1             Bonds, Barry
2    Columbus, Christopher
3            Defoe, Daniel
4          Estevez, Emilio
5         Flintstone, Fred
6             Garbo, Greta
7         Humbert, Humbert
8              Ilych, Ivan

有人建议这样的代码,但它没有工作......

names.apply(split)[1]+', ' + names.apply(split)[0]

我检查了以下线程,但它们似乎也不是我想要的:

Pandas DataFrame, how do i split a column into two

pandas: How do I split text in a column into multiple rows?

【问题讨论】:

    标签: python pandas series


    【解决方案1】:

    使用和不使用str.replace

    In [451]: names.str.split().apply(lambda x: ', '.join(x[::-1]))
    Out[451]:
    0            Agassi, Andre
    1             Bonds, Barry
    2    Columbus, Christopher
    3            Defoe, Daniel
    4          Estevez, Emilio
    5         Flintstone, Fred
    6             Garbo, Greta
    7         Humbert, Humbert
    8              Ilych, Ivan
    dtype: object
    
    In [452]: names.apply(lambda x: ', '.join(x.split()[::-1]))
    Out[452]:
    0            Agassi, Andre
    1             Bonds, Barry
    2    Columbus, Christopher
    3            Defoe, Daniel
    4          Estevez, Emilio
    5         Flintstone, Fred
    6             Garbo, Greta
    7         Humbert, Humbert
    8              Ilych, Ivan
    dtype: object
    

    【讨论】:

      【解决方案2】:

      矢量化 Numpy 解决方案:

      In [276]: arr = names.str.split(expand=True).values[:, ::-1]
      
      In [277]: names.values[:] = np.sum(np.insert(arr, 1, ', ', axis=1), axis=1)
      
      In [278]: names
      Out[278]:
      0            Agassi, Andre
      1             Bonds, Barry
      2    Columbus, Christopher
      3            Defoe, Daniel
      4          Estevez, Emilio
      5         Flintstone, Fred
      6             Garbo, Greta
      7         Humbert, Humbert
      8              Ilych, Ivan
      dtype: object
      

      【讨论】:

        【解决方案3】:

        使用 .map 结合字符串方法,如下所示:

        names.map(lambda s: s.split()[1] + ', ' + s.split()[0])
        

        【讨论】:

          【解决方案4】:

          首先,使用 .split 方法定义一个函数来反转名称。它采用您想要将其拆分的参数,在本例中为“”,并返回输入字符串的两个部分的列表。从那里我们可以按照我们喜欢的方式重新组织函数的返回字符串——在本例中是姓氏,名字。

          其次,reverse_names 函数接收一个 Pandas 系列,将函数 reverse_name 应用于系列中的每个元素(使用 .apply 方法),然后返回另一个 Pandas 系列。

          def reverse_name(name):
              split_name = name.split(" ")
              first_name = split_name[0]
              last_name = split_name[1]
              return last_name + ", " + first_name
          
          def reverse_names(names):
              return names.apply(reverse_name)
          
          print reverse_names(names)
          

          你的输出应该是这样的:

          0             Agassi, Andre
          1              Bonds, Barry
          2     Columbus, Christopher
          3             Defoe, Daniel
          4           Estevez, Emilio
          5          Flintstone, Fred
          6              Garbo, Greta
          7          Humbert, Humbert
          8               Ilych, Ivan
          9              Joyce, James
          10         Knightley, Keira
          11               Lane, Lois
          12              Myers, Mike
          13              Nolte, Nick
          14           Osbourne, Ozzy
          15           Picasso, Pablo
          16       Quirrell, Quirinus
          17             Ray, Rachael
          18          Sarandon, Susan
          19             Turner, Tina
          20           Urbina, Ugueth
          21            Vaughn, Vince
          22          Wilson, Woodrow
          23             Yamada, Yoji
          24         Zidane, Zinedine
          dtype: object
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-01-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-06-21
            • 1970-01-01
            相关资源
            最近更新 更多