2
我有一個數據幀,其中有列id, uid, gid, tstamp
。我能夠做df[df['id'] == 12]
這給查找特定行:在熊貓中查找行並更新具體的值
id uid gid tstamp
711 12 CA CA-1 47585768600
如何更新的uid
價值和gid
從CA
到IN
和CA-1
到IN-1
說?
我有一個數據幀,其中有列id, uid, gid, tstamp
。我能夠做df[df['id'] == 12]
這給查找特定行:在熊貓中查找行並更新具體的值
id uid gid tstamp
711 12 CA CA-1 47585768600
如何更新的uid
價值和gid
從CA
到IN
和CA-1
到IN-1
說?
您可以通過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