2016-12-14 25 views
0

在我的列中,我有幾個國家/地區名稱,其名稱中包含數字和/或括號,需要刪除。如何從DataFrame列中的名稱中刪除數字和/或括號

例如:

-'Bolivia(多民族國)」應該是 '玻利維亞'

-'Switzerland17' 應該是 '瑞士'

所討論的列也被設置爲我的索引是否會影響事物?

+0

向我們展示你嘗試過什麼。 SO不是代碼寫入服務 –

回答

2

試試這個:

In [121]: df 
Out[121]: 
            expected 
Bolivia (Plurinational State of)  Bolivia 
Switzerland17      Switzerland 

In [122]: df.set_index(df.index.str.replace('\s*\(.*?\)\s*', '').str.replace('\d+',''), inplace=True) 

In [123]: df 
Out[123]: 
       expected 
Bolivia   Bolivia 
Switzerland Switzerland 

In [124]: df.index == df.expected 
Out[124]: array([ True, True], dtype=bool) 

In [125]: (df.index == df.expected).all() 
Out[125]: True