Python 从熊猫索引中获取类

Python 从熊猫索引中获取类,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有两个数据帧 df1=pd.DataFrame({'index':[1,2,3,4],'Name':['Andi','Boby','Charlie','Daniel'],'Occupation':['x','xxx','xxx','x']}) 及 基于索引,我想得到基于索引的类,所以我通过合并df1和df2创建了另一个数据帧。我曾经 data1=df1.merge(df2,on='index',how='left') 结果是我有两列职业x和职业y。如何合并数据帧而不使用那些占领x和占领y列

我有两个数据帧

df1=pd.DataFrame({'index':[1,2,3,4],'Name':['Andi','Boby','Charlie','Daniel'],'Occupation':['x','xxx','xxx','x']})

基于索引,我想得到基于索引的类,所以我通过合并df1和df2创建了另一个数据帧。我曾经

data1=df1.merge(df2,on='index',how='left')

结果是我有两列职业x和职业y。如何合并数据帧而不使用那些占领x和占领y列,因此这些列将是索引、名称、占领、类

,而合并时指定
以避免占领x和占领y

您可以仅使用df2中的索引和类进行连接

data1=df1.join(df2[['Class']])

尝试此操作,在合并前添加此行

df2.pop('Occupation') # this line needs to be added
data1=df1.merge(df2,on='index', how='left')
如果有多个这样的列,请尝试以下方法

cols = ['Col1', 'Col2'] # Add required columns of df2 here
data1=df1.merge(df2[cols],on='index', how='left')

删除Occupation_x列并将Occupation_y列重命名为OccupationYes,这将是一项简单的工作,但当特性超过10个时,您将遇到问题。谢谢,假设我们有10多个功能,有没有办法避免制作功能1_x,功能1_y。使用right_on或left_on,我们需要指定所有的feaures。您可以在合并时使用
后缀=(''ux',''uy')
,将构成为col1_xcol1_y的重复列的名称设置为col和colu any_u字符串,然后通过regex('u any_u字符串)删除这些列。谢谢,我正在寻找的内容。
cols = ['Col1', 'Col2'] # Add required columns of df2 here
data1=df1.merge(df2[cols],on='index', how='left')