2016-07-12 91 views
1

我有一個數據集,其中兩列的字符串值爲「LastName,FirstName」。我很想用「名字姓氏」替換它們。他們是這樣的:如何在不丟失數據框的情況下編輯LastName,FirstName的順序

 conductorName  composerName conduct_count 
0 Abbado, Claudio   Berg, Alban    2 
1 Abbado, Claudio  Berlioz, Hector    1 
2 Abbado, Claudio Bernstein, Leonard    1 
3 Abbado, Claudio Brahms, Johannes    2 
4 Abbado, Claudio  Bruckner, Anton    1 

我曾嘗試: data = [" ".join(n.split(", ")[::-1]) for n in data["composerName"]]

這很好地給我composerName的名單,但現在我失去了大熊貓據幀(這裏是錯誤----> 1 data.head() AttributeError: 'list' object has no attribute 'head'

我有也嘗試過: for n in data["composerName"]: " ".join(n.split(", ")[::-1])

但是,這並不改變姓氏和名字的順序

我很想保留熊貓數據框進行分析。我很感激任何幫助。謝謝!!

+0

也許'data ['conductorName'] = data ['conductorName']。str.replace(',','')' – mtoto

+0

謝謝。這很快,而且效果很好。 – achimneyswallow

回答

3

可以使用矢量化str.split分裂在隔板上,然後用切片語義顛倒列表的內容,然後加入再次使用str.join

In [35]: 
df['ComposerFirstLastName'] = df['composerName'].str.split(', ').str[::-1].str.join(' ') 
df['ConductorFirstLastName'] = df['conductorName'].str.split(', ').str[::-1].str.join(' ') 
df 
Out[35]: 
      composerName  conductorName conductor_count FirstLastName \ 
index                   
0  Abbado, Claudio   Berg, Alban    2 Claudio Abbado 
1  Abbado, Claudio  Berlioz, Hector    1 Claudio Abbado 
2  Abbado, Claudio Bernstein, Leonard    1 Claudio Abbado 
3  Abbado, Claudio Brahms, Johannes    2 Claudio Abbado 
4  Abbado, Claudio  Bruckner, Anton    1 Claudio Abbado 

     ComposerFirstLastName ConductorFirstLastName 
index            
0   Claudio Abbado    Alban Berg 
1   Claudio Abbado   Hector Berlioz 
2   Claudio Abbado  Leonard Bernstein 
3   Claudio Abbado  Johannes Brahms 
4   Claudio Abbado   Anton Bruckner 
+0

謝謝,這個作品! – achimneyswallow

1

你改寫了在整個數據框:

data = [" ".join(n.split(", ")[::-1]) for n in data["composerName"]] 

這是它應該說:

data["composerName"] = [" ".join(n.split(", ")[::-1]) for n in data["composerName"]] 
+0

感謝您的意見。 – achimneyswallow

相關問題