10

I am using python 3 and jupyter notebook. I have a pandas dataframe that is structured like this:

          location  price
Apr 25   ASHEVILLE   15.0
Apr 25   ASHEVILLE   45.0
Apr 25   ASHEVILLE   50.0
Apr 25   ASHEVILLE  120.0
Apr 25   ASHEVILLE  300.0
<class 'pandas.core.frame.DataFrame'>

I am simply trying to create a boxplot for each location to show the range of prices among items in each location.

When I ran the following code:

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline


plt.boxplot(postings)
plt.show()

It returned TypeError: unhashable type: 'slice'

2
  • are you sure postings is a dataframe? try just passing the values of the column and not the whole dataframe. Commented May 22, 2017 at 18:32
  • It is a df. When I try to pass plt.boxplot(postings.location) instead, it outputs IndexError: 0 Commented May 22, 2017 at 18:43

3 Answers 3

13

I guess you require boxplot for each location in same graph. I modified given dataframe to add sample data for another location which looks like-

   date   location month  price
0    25  ASHEVILLE   Apr   15.0
1    25  ASHEVILLE   Apr   45.0
2    25  ASHEVILLE   Apr   50.0
3    25  ASHEVILLE   Apr  120.0
4    25  ASHEVILLE   Apr  300.0
5    25  NASHVILLE   Apr   34.0
6    25  NASHVILLE   Apr   55.0
7    25  NASHVILLE   Apr   70.0
8    25  NASHVILLE   Apr  105.0
9    25  NASHVILLE   Apr   85.0

Now, just call boxplot on this frame and provide parameters- column and by

postings.boxplot(column='price', by='location')

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

3

I guess "price" is the column of data that you want to have boxplotted. So you need to first select this column and supply only that column to plt.boxplot.

u = u"""index,location,price
    Apr 25,ASHEVILLE,15.0
    Apr 25,ASHEVILLE,45.0
    Apr 25,ASHEVILLE,50.0
    Apr 25,ASHEVILLE,120.0
    Apr 25,ASHEVILLE,300.0"""

import io
import pandas as pd
import matplotlib.pyplot as plt

data = io.StringIO(u)

df = pd.read_csv(data, sep=",", index_col=0)

plt.boxplot(df["price"])
plt.show()

enter image description here

Comments

1

Juding from the data, you want to have a boxplot with a single box from the 5 price values you have. You need to pass the actual data you want to make the boxplot from.

plt.boxplot(postings["price"])

Check out the examples here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.