问题描述
我有两个数据框(df1
、df2
),我想在 df1
中创建一个新列,在 "Slave"
中使用 { {1}} 中的 df2
并将该行的 "Name"
插入到 df1
中的同一行中。
"FullName"
df1
谢谢
解决方法
import pandas as pd
data1 =[[0,"#INV1","ESN:21010730236TJ5902772"],[1,"#INV3","ESN:21010730236TJ5902787"],[2,"#INV5","ESN:21010730236TJ5902785"],[3,"#INV2","ESN:21010730236TJ5902800"],[4,"#INV4","ESN:21010730236TJ5902784"]]
data2=[[0,"21010730236TJ5900031",1,"1.1.1"],"21010730236TJ5902800",2,"1.1.2"],"21010730236TJ5902787",3,"1.1.3"],"21010730236TJ5902784",4,"1.1.4"]]
df1 = pd.DataFrame(data1,columns=["SN","num","Name"])
df2 = pd.DataFrame(data2,"Slave","Add","FullName"])
您可以执行以下操作:
#remove the ESN: from df1
df1["Name"]=df1["Name"].str.replace("ESN:","")
#make a new DataFrame merging the df1 and df2
result =pd.merge(left=df1,right=df2,how='left',left_on='Name',right_on='Slave')
#drop the columns of the df2 from the result DataFrame
result.drop(['SN_y','Add',"Slave"],axis=1,inplace=True)
#rename the columns of the result DataFrame to match the df1
result.columns=["SN","Name","FullName"]
#adding the ESN: again
result["Name"]="ESN:"+result["Name"]
这就是 result
DataFrame 的样子(检查您想要索引的方式):
SN num Name FullName
0 0 #INV1 ESN:21010730236TJ5902772 NaN
1 1 #INV3 ESN:21010730236TJ5902787 1.1.3
2 2 #INV5 ESN:21010730236TJ5902785 NaN
3 3 #INV2 ESN:21010730236TJ5902800 1.1.2
4 4 #INV4 ESN:21010730236TJ5902784 1.1.4