简体   繁体   English

将两个 dataframe Python 与不同的列合并

[英]merge two dataframe Python with different columns

I trying to concat in python the following datasets:我试图在 python 中连接以下数据集:

df1: df1:

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

df2: 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:我想 append 列 df1.cwe,df1.description, df1.cvss 和 df2.Title, df2.Description 和 df2.CVSS Temporal Score 的信息得到以下情况:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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