Python Pandas AttributeError:“系列”对象没有属性“列”

时间:2019-10-28 11:21:31

标签: python pandas

此代码:

import pandas as pd

data = pd.read_csv('house', sep="\t", header=None)
data.columns = ['label', 'msg']
data['msg_length'] = data['msg'].apply(lambda x: len(x))
data['msg'].hist(column =data['msg_length'], by=data['label'], bins=50)

给我这个错误:

AttributeError:“系列”对象没有属性“列”

我用pd.DataFrame和pd.Series尝试了不同的方法,但是没有运气。 我怎么了?

pd.read_csv的结果: enter image description here

预期输出: enter image description here

错误代码:

enter image description here

2 个答案:

答案 0 :(得分:1)

DataFrame.hist与字符串列一起使用,而不是Series,例如data['msg_length']

data.hist(column ='msg_length', by='label', bins=50)

答案 1 :(得分:0)

data.hist(column ='msg_length', by='label')  

完成工作