pandas 为什么会出现错误 - 无法连接非 NDFrame 对象

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

Why is it giving the error-cannot concatenate a non-NDFrame object

pythonpandas

提问by VaibhavSka

The fill function is to fill all the non-available data in titanic with the average age of that particular class of passengers

填充功能是用该特定类别乘客的平均年龄填充泰坦尼克号中所有不可用的数据

titanic['Age']=titanic[['Age','Pclass']].apply(fill,axis=1) 

sex=pd.get_dummies(titanic['Sex'],drop_first=True)

embarked=pd.get_dummies(titanic['Embarked'],drop_first=True)

titanic.drop(['Cabin','Embarked'],axis=1,inplace=True)

titanic.dropna(inplace=True)

titanic=pd.concat(['titanic','sex','embarked'],axis=1)

回答by Bharath

pd.concat expects you to give list of dataframe as parameter not a list of strings. Change your code to

pd.concat 期望您将数据帧列表作为参数而不是字符串列表。将您的代码更改为

titanic=pd.concat([titanic,sex,embarked],axis=1)

回答by Rayhane Mama

I think you mean:

我想你的意思是:

titanic=pd.concat([titanic,sex,embarked],axis=1)

instead of:

代替:

titanic=pd.concat(['titanic','sex','embarked'],axis=1)

A side note however:
If you're just trying to drop NaNvalues and get dummies you can do it on the original dataframe without having to use pandas.concat.

但是,附带说明:
如果您只是想删除NaN值并获取虚拟对象,则可以在原始数据帧上进行操作,而无需使用pandas.concat

example:

例子:

titanic = pd.get_dummies(titanic)

and just drop NaN values with dataframe.dropna:

只需使用dataframe.dropna删除 NaN 值:

titanic.dropna(inplace=True)

Hope this was helpful.

希望这是有帮助的。