2016-02-17 57 views
3

假設我有一個包含一系列profile從列表替換熊貓系列的值

settings-win.data.microsoft.com 1 
www.facebook.com     0.4 
clients4.google.com    1 
plus.google.com     0.86 

而且包含列表new_val

[0.8408964152537145, 0, 1.5, 0] 

如何更換所有值我的profile系列與清單?它應該是:

settings-win.data.microsoft.com 0.8408964152537145 
www.facebook.com     0 
clients4.google.com    1.5 
plus.google.com     0 

我試過的Series.replace(),但它似乎並沒有工作。

PS。我也很困惑如何以及何時使用.replace(),所以如果你可以進一步解釋它,它將非常感激。

+0

'profile.values = new_val' –

回答

2

我認爲你可以用舊Seriessindex和列表li的價值創造新Series

print s 
settings-win.data.microsoft.com 1.00 
www.facebook.com     0.40 
clients4.google.com    1.00 
plus.google.com     0.86 
Name: profile, dtype: float64 

new_val = [0.8408964152537145, 0, 1.5, 0] 
s1 = pd.Series(new_val, index=s.index, name='profile') 

print s1 
settings-win.data.microsoft.com 0.840896 
www.facebook.com     0.000000 
clients4.google.com    1.500000 
plus.google.com     0.000000 
Name: profile, dtype: float64 

但也許更好的是dictionaryreplacemap,但結果是不同的,因爲第一和第三值是相同的 - doc

d = {1: 0.8408964152537145, 0.4: 0, 0.86: 0} 
print d 
{1: 0.8408964152537145, 0.4: 0, 0.86: 0} 

print s.replace(d) 
settings-win.data.microsoft.com 0.840896 
www.facebook.com     0.000000 
clients4.google.com    0.840896 
plus.google.com     0.000000 
Name: profile, dtype: float64 

print s.map(d) 
settings-win.data.microsoft.com 0.840896 
www.facebook.com     0.000000 
clients4.google.com    0.840896 
plus.google.com     0.000000 
Name: profile, dtype: float64