Python Pandas 合并给出错误“缓冲区的维数错误(预期为 1,得到 2)”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27065133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas merge giving error "Buffer has wrong number of dimensions (expected 1, got 2)"
提问by lathomas64
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.
我正在尝试进行熊猫合并,并在尝试运行它时从标题中获取上述错误。我使用 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:
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'section_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_principal_school_phone' ',你'school_asst_principal_phone', u'school_ass t_principal_email'], dtype='object')
c:
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', u's cool_asst_principal_email'], dtype='object
Is it possible to merge on three columns like this? Is there anything wrong from the merge call here?
是否可以像这样合并三列?这里的合并调用有什么问题吗?
采纳答案by JD Long
As mentioned in the comments, you have a dupe column:
正如评论中提到的,你有一个欺骗列:


回答by 2Obe
To adress the issue of the dupe columns you can either drop the dupe column using duplicatedwith smth. like:
要解决dupe 列的问题,您可以使用duplicatedwith smth删除dupe 列。喜欢:
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))]
或使用例如:
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
请记住,在这种情况下,您必须调整合并部分
回答by Shivpe_R
This Will remove the duplicated columns from the Dataframe
这将从数据框中删除重复的列
df = df[list(df.columns[~df.columns.duplicated()])]

