I have the following CSV files:
file1.csv #dataframe is named dfFile1
Id,name,pos_neg,line
1,abc,pos,cas
2,cde,neg,work
3,efg,pos,cat
4,abc,pos,job
file2.csv #dataframe is named dfFile2
Id,ref,names,other
c10,n1,www,10.5
c11,m4,efg,5.4
c12,m5,cde,9.8
c13,m9,hhh,6.7
c14,n4,abc,12.5
c15,n9,kkk,3.4
which I converted into dataframes using pandas. I would like to obtain a third data frame that matches the rows of dfFile2 according to the unique values presented in the name field of dfFile1, and also add the pos_neg row from file 1, so I will end up with:
dfNew
Id,ref,names,other,pos_neg
c11,m4,efg,5.4,pos
c12,m5,cde,9.8,neg
c14,n4,abc,12.5,pos
So far, I have done the following:
list=[]
list=dfFile1["name"].unique() #contains [abc,cde,efg]
dfFile2=dfFile2[dfFile2.names.isin(list)]
However, I just do not know how can I merge the column pos_neg from dfFile1, I tried the following:
dfNew=dfFile2.merge(dfFile2,dfFil1[["pos_neg"]],on=dfFile2)
Unfortunately, it does not work.