栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

How to Solve Python AttributeError: ‘DataFrame’ object has no attribute ‘concat’

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

How to Solve Python AttributeError: ‘DataFrame’ object has no attribute ‘concat’

解决问题出处:https://researchdatapod.com/how-to-solve-python-attributeerror-dataframe-object-has-no-attribute-concat/#:~:text=AttributeError%20occurs%20in%20a%20Python%20program%20when%20we,are%20handling%20does%20not%20have%20the%20concat%20attribute.

能阅读英文的可以移步以上链接。

中文版:

这个出错的问题在于,DataFrame 根本就没有 concat 这个方法,concat方法是pandas的内置方法。简单点理解就是,“动物”的对象有猴子,“动物”有“飞、跳、跑、游”的方法,但是猴子不是孙悟空它不会飞。

那怎么办?我就是想让它飞??

没关系,可以滴!

先看例子:

import pandas as pd

pizza_names = ['margherita', 'pepperoni', 'four cheeses', 'parmigiana', 'hawaiian', 'marinara']

is_vegetarian = [True, False, True, True, False, True]

df1 = pd.DataFrame(zip(pizza_names, is_vegetarian), columns=['pizza_names', 'is_vegetarian'])

df2 = pd.DataFrame({'prices':[7.99, 8.99, 8.99, 9.99, 9.99, 6.99]})

print(df1)
print(df2)

 

 首先,我们强行让这个猴子飞:

df3 = df1.concat(columns=df2['prices'], axis=1)

print(df3)

 很遗憾,它被你摔死了!“AttributeError: 'DataFrame' object has no attribute 'concat'”

都说了它不会飞不会飞!(DataFrame 中没有 concat 方法!)

 其实你可能不理解,为什么孙悟空可以飞?因为它有精斗云啊,所以,我们只需要给它精斗云就可以啦。。。

df3 = pd.concat([df1, df2], axis=1)

print(df3)

print(type(df3))

~~~

明白了吧,pandas就是那个精斗云,厉害的紧!

ps:你可能会好奇,axis参数是干嘛的,0代表横着飞,1代表竖着飞~ 试试不就知道了嘛 ^.^ 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/968731.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号