I am trying to do a pandas merge and get the above error from the title when I try to run it. I am using 3 columns to match on whereas just before I do similar merge on only 2 columns and it works fine.
我正在尝试做一个pandas merge并在我尝试运行时从标题中获得上述错误。我正在使用3列匹配,而在我仅在2列上进行类似合并之前它工作正常。
df = pd.merge(df, c, how="left",
left_on=["section_term_ps_id", "section_school_id", "state"],
right_on=["term_ps_id", "term_school_id", "state"])
columns for the two dataframes
两个数据帧的列
df:
Index([u'section_ps_id', u'section_school_id', u'section_course_number', u'secti on_term_ps_id', u'section_staff_ps_id', u'section_number', u'section_expression' , u'section_grade_level', u'state', u'sections_id', u'course_ps_id', u'course_sc hool_id', u'course_number', u'course_schd_dept', u'courses_id', u'school_ps_id', u'course_school_id', u'school_name', u'school_abbr', u'school_low_grade', u'sch ool_high_grade', u'school_alt_school_number', u'school_state', u'school_phone', u'school_fax', u'school_principal', u'school_principal_phone', u'school_principa l_email', u'school_asst_principal', u'school_asst_principal_phone', u'school_ass t_principal_email'], dtype='object')
索引([u'section_ps_id',u'section_school_id',u'section_course_number',u'secti on_term_ps_id',u'section_staff_ps_id',u'section_number',u'section_expression',u'section_grade_level',u'state',u 'sections_id',u'course_ps_id',u'course_sc hool_id',u'course_number',u'course_schd_dept',u'courses_id',u'school_ps_id',u'course_school_id',u'school_name',u'school_abbr', u'school_low_grade',u'sch ool_high_grade',u'school_alt_school_number',u'school_state',u'school_phone',u'school_fax',u'school_principal',u'school_principal_phone',u'school_principa l_email',u'school_asst_principal ',u'school_asst_principal_phone',u'school_ass t_principal_email'],dtype ='object')
c:
Index([u'term_ps_id', u'term_school_id', u'term_portion', u'term_start_date', u' term_end_date', u'term_abbreviation', u'term_name', u'state', u'terms_id', u'sch ool_ps_id', u'term_school_id', u'school_name', u'school_abbr', u'school_low_grad e', u'school_high_grade', u'school_alt_school_number', u'school_state', u'school _phone', u'school_fax', u'school_principal', u'school_principal_phone', u'school _principal_email', u'school_asst_principal', u'school_asst_principal_phone', u's chool_asst_principal_email'], dtype='object')
索引([u'term_ps_id',u'term_school_id',u'term_portion',u'term_start_date',u'term_end_date',u'term_abbreviation',u'term_name',u'state',u'terms_id',u' sch ool_ps_id',u'term_school_id',u'school_name',u'school_abbr',u'school_low_grad e',u'school_high_grade',u'school_alt_school_number',u'school_state',u'school _phone',u'school_fax' ,u'school_principal',u'school_principal_phone',u'school _principal_email',u'school_asst_principal',u'school_asst_principal_phone',你的chool_asst_principal_email'],dtype ='object')
Is it possible to merge on three columns like this? Is there anything wrong from the merge call here?
是否可以像这样合并三列?这里的合并电话有什么问题吗?
3 个解决方案
#1
36
As mentioned in the comments, you have a dupe column:
正如评论中所提到的,你有一个重叠列:
#2
2
This Will remove the duplicated columns from the Dataframe
这将从Dataframe中删除重复的列
df = df[list(df.columns[~df.columns.duplicated()])]
#3
0
To adress the issue of the dupe columns you can either drop the dupe column using duplicated with smth. like:
要解决欺骗列的问题,您可以使用带有smth的重复删除欺骗列。喜欢:
c = c[~c.columns.duplicated(keep='first')]
or adding an additional char to either one of the DataFrames using for example: c.columns=[c.columns[i]+str(i) for i in range(len(c.columns))]
或者使用一个额外的char添加到其中一个DataFrames,例如:c.columns = [c.columns [i] + str(i)for i in range(len(c.columns))]
Keep in mind that in this case you must adjust the merging part
请记住,在这种情况下,您必须调整合并部分
#1
36
As mentioned in the comments, you have a dupe column:
正如评论中所提到的,你有一个重叠列:
#2
2
This Will remove the duplicated columns from the Dataframe
这将从Dataframe中删除重复的列
df = df[list(df.columns[~df.columns.duplicated()])]
#3
0
To adress the issue of the dupe columns you can either drop the dupe column using duplicated with smth. like:
要解决欺骗列的问题,您可以使用带有smth的重复删除欺骗列。喜欢:
c = c[~c.columns.duplicated(keep='first')]
or adding an additional char to either one of the DataFrames using for example: c.columns=[c.columns[i]+str(i) for i in range(len(c.columns))]
或者使用一个额外的char添加到其中一个DataFrames,例如:c.columns = [c.columns [i] + str(i)for i in range(len(c.columns))]
Keep in mind that in this case you must adjust the merging part
请记住,在这种情况下,您必须调整合并部分