How to reverse the order of first and last name in a Pandas Series

前端 未结 4 862
太阳男子
太阳男子 2021-01-24 16:42

I have a pandas series:

names = pd.Series([
\'Andre Agassi\',
\'Barry Bonds\',
\'Christopher Columbus\',
\'Daniel Defoe\',
\'Emilio Estevez\',
\'Fred Flintstone\         


        
相关标签:
4条回答
  • 2021-01-24 16:57

    Vectorized Numpy solution:

    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
    
    0 讨论(0)
  • 2021-01-24 17:02

    Use .map combined with string methods like below:

    names.map(lambda s: s.split()[1] + ', ' + s.split()[0])
    
    0 讨论(0)
  • 2021-01-24 17:10

    First, define a function to reverse the name, utilizing the .split method. It takes the parameter where you want to split it at, in this case " " and returns a list of the two parts of your input string. From there we can reorganize the return string of our function how we like--in this case last name, first name.

    Second, the reverse_names function takes in a Pandas series, applies the function reverse_name to each element in the series (with the .apply method) and then returns another Pandas Series.

    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)
    

    Your output should be something like this:

    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
    
    0 讨论(0)
  • 2021-01-24 17:12

    With and without using 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
    
    0 讨论(0)
提交回复
热议问题