2016-11-01 47 views
2

我有一個數據幀,其中有列id, uid, gid, tstamp。我能夠做df[df['id'] == 12]這給查找特定行:在熊貓中查找行並更新具體的值

 id uid gid tstamp 
711 12 CA CA-1 47585768600 

如何更新的uid價值和gidCAINCA-1IN-1說?

回答

2

您可以通過ix選擇和設定值['IN','IN-1']

print (df) 
    id uid gid  tstamp 
711 12 CA CA-1 47585768600 
711 15 CA CA-1 47585768600 

df.ix[df['id'] == 12, ['uid','gid']] = ['IN','IN-1'] 

print (df) 
    id uid gid  tstamp 
711 12 IN IN-1 47585768600 
711 15 CA CA-1 47585768600 

另一種解決方案與replace

df.ix[df['id'] == 12, ['uid','gid']] = 
df.ix[df['id'] == 12, ['uid','gid']].replace({'CA':'IN'}, regex=True) 

print (df) 
    id uid gid  tstamp 
711 12 IN IN-1 47585768600 
711 15 CA CA-1 47585768600