radonrace
radonrace

Reputation: 61

Python Pandas AttributeError: 'Series' object has no attribute 'columns'

This code :

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)

Gives me this error:

AttributeError: 'Series' object has no attribute 'columns' `

I have tried different things with pd.DataFrame and pd.Series, without luck. What do i wrong?

Result of pd.read_csv: enter image description here

Expected output: enter image description here

Error code:

enter image description here

Upvotes: 1

Views: 21474

Answers (2)

radonrace
radonrace

Reputation: 61

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

did the job

Upvotes: 0

jezrael
jezrael

Reputation: 862611

Use DataFrame.hist with columns in strings instead Series like data['msg_length']:

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

Upvotes: 1

Related Questions