简体   繁体   中英

merge two dataframe Python with different columns

I trying to concat in python the following datasets:

df1:

class,     cvss,  cwe,  description,  line,  reccomandation,   vul_id
a.class    cvss1    cwe1,   desc1,    l1,        rec1,          vul1

df2:

Title,NTimes,Synopsis,Description,Solution,Risk Factor,CVSS Temporal Score
tit1, nt1     syn1         desc2  sol1      risk1         cvss2
tit2, nt2     syn2         desc3  sol2     risk2          cvss3

I want to append the information of the columns df1.cwe,df1.description, df1.cvss with df2.Title, df2.Description and df2.CVSS Temporal Score to obtains the following situation:

class,   line,reccomandation,vul_id,Title,NTimes,Synopsis,Description,Solution,Risk Factor,CVSS Temporal Score
a.class  l1   rec1           vul1   cwe1,                  desc1                            cvss1     
                                    tit1   nt1     syn1    desc2      sol1      risk1       cvss2
                                    tit2   nt2     syn2    desc3      sol2      risk2       cvss3

I have tried using merge but without success using the follow code:

 res= pd.merge(df1,df2 left_on=['cwe','description','cvss'],right_on=['Title','Description','CVSS Temporal Score']) 

Just use the following approach:

#create new empty data frame 
df = pd.DataFrame()

#add the columns from df1 to df
df['cwe']=df1['cwe']
#add the rest of the columns from df1 to df as above

#add the columns from df2 to df
df['title']=df1['title']
#add the rest of the columns from df2 to df as above

#view the new dataframe
df

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM