Skip to content Skip to sidebar Skip to footer

How To Reverse The Order Of First And Last Name In A Pandas Series

I have a pandas series: names = pd.Series([ 'Andre Agassi', 'Barry Bonds', 'Christopher Columbus', 'Daniel Defoe', 'Emilio Estevez', 'Fred Flintstone', 'Greta Garbo', 'Humbert Humb

Solution 1:

With and without using str.replace?

In[451]: names.str.split().apply(lambda x: ', '.join(x[::-1]))
Out[451]:
0Agassi, Andre1Bonds, Barry2Columbus, Christopher3Defoe, Daniel4Estevez, Emilio5Flintstone, Fred6Garbo, Greta7Humbert, Humbert8Ilych, Ivandtype: objectIn[452]: names.apply(lambda x: ', '.join(x.split()[::-1]))
Out[452]:
0Agassi, Andre1Bonds, Barry2Columbus, Christopher3Defoe, Daniel4Estevez, Emilio5Flintstone, Fred6Garbo, Greta7Humbert, Humbert8Ilych, Ivandtype: object

Solution 2:

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

Solution 3:

Use .map combined with string methods like below:

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

Solution 4:

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.

defreverse_name(name):
    split_name = name.split(" ")
    first_name = split_name[0]
    last_name = split_name[1]
    return last_name + ", " + first_name

defreverse_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

Post a Comment for "How To Reverse The Order Of First And Last Name In A Pandas Series"