2017-02-12 36 views
1

我有熊猫系列:如何扭转姓和名的顺序在熊猫系列

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?

回答

4

有没有使用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

使用.MAP结合串方法如下图所示:

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

矢量化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 
0

首先,定义一个函数来扭转的名称,利用该方法.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 

一张纸条,内容从Udacity来了。他们对此内容和解决方案拥有完全的所有权。