pandas Python 熊猫:“无法将 DataFrame 与 <class 'str'> 类型的实例合并”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49127961/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:16:54  来源:igfitidea点击:

Python pandas: "can not merge DataFrame with instance of type <class 'str'>"

pythonpandas

提问by William Feirie

I have two df: df_jan_2001and df_feb_2001. I would like to do a full outer join by using this syntax:

我有两个 df:df_jan_2001df_feb_2001. 我想使用以下语法进行完整的外部联接:

new_df = pd.merge('df_jan2001', 'df_feb2001', how='outer', left_on=
['designation', 'name'], right_on=['designation', 'name'])

designationand nameare both string variables.

designationname都是字符串变量。

Why do I get the following error and how can I fix it?

为什么会出现以下错误,我该如何解决?

"ValueError: can not merge DataFrame with instance of type <class 'str'>"

Sorry if this is a basic question, just getting started with pandas.

对不起,如果这是一个基本问题,刚刚开始使用Pandas。

回答by William Feirie

YOU can try like this.

你可以这样试试。

new_df = pd.merge(df_jan2001, df_feb2001, how='outer', left_on=['designation', 'name'], right_on=['designation', 'name'])

回答by Utopia

Try using new_df = pd.merge(df_jan2001.to_frame(), df_feb2001.to_frame(), how='outer', left_on=['designation', 'name'], right_on=['designation', 'name'])

尝试使用 new_df = pd.merge(df_jan2001.to_frame(), df_feb2001.to_frame(), how='outer', left_on=['designation', 'name'], right_on=['designation', 'name'])

回答by jezrael

There is problem 'df_jan2001'is string, df_jan2001is here DataFrame.

有问题'df_jan2001'stringdf_jan2001就在这里DataFrame

If columns are same in both DataFrames, use parameter ononly:

如果两个DataFrames 中的列相同,则on仅使用参数:

new_df = pd.merge(df_jan2001, df_feb2001, how='outer', on=['designation', 'name'])