How to append value with replace value in column no2, and i tried like appending my existing value with replace value, but not working.
import pandas as pd
df=pd.DataFrame({'no1':[20,20,40,10,50],
'no2':[123,20,123,40,50],
'no3':[30,10,50,40,50]})
lookup=pd.DataFrame({'Name':['A','B','C','D','E'],
'id':[123,20,30,40,50]})
df['no2']+str(df['no2'].replace(dict(zip(lookup.id, lookup.Name)), inplace =True))
df
the output i am getting-
no1 no2 no3
0 20 A 30
1 20 B 10
2 40 A 50
3 10 D 40
4 50 E 50
but i need output like this- - (appending my existing value of no2 with lookup value)
no1 no2 no3
0 20 123-A 30
1 20 20-B 10
2 40 123-A 50
3 10 40-D 40
4 50 50-E 50
try via replace()
:
df['no2']=df['no2'].astype(str)+'-'+df['no2'].replace(dict(lookup[['id','Name']].values))
OR
try via map()
:
df['no2']=df['no2'].astype(str)+'-'+df['no2'].map(dict(lookup[['id','Name']].values))
Note:
#you can also replace this code:
dict(lookup[['id','Name']].values)
#to:
lookup.set_index('id')['Name']
试试这个:
df['no2'] = df['no2'] + '-' + lookup['id']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.