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'
Practical Data Science using Python Box Plot in Seaborn is used to draw a box plot to show distributions with respect to categories. To create a vertical Box Plot, use the seaborn. boxplot().
boxplot . Some of the frequently used ones are, main -to give the title, xlab and ylab -to provide labels for the axes, col to define color etc. Additionally, with the argument horizontal = TRUE we can plot it horizontally and with notch = TRUE we can add a notch to the box.
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')
 
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With