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

拥有回忆 提交于 2019-12-20 04:53:50

问题


I have a pandas series:

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

Which looks like this:

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

and I want to make it 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

Someone suggested code like this, but it didn't work...

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

I checked the following threads, but they didn't seem to be what I wanted either:

Pandas DataFrame, how do i split a column into two

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


回答1:


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



回答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



回答3:


Use .map combined with string methods like below:

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



回答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.

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


来源:https://stackoverflow.com/questions/42189469/how-to-reverse-the-order-of-first-and-last-name-in-a-pandas-series

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!