I have a Pandas DataFrame, df
, that has a price
column and a year
column. I want to create a boxplot after grouping the rows based on their year
. Here’s an example:
JavaScript
x
19
19
1
import pandas as pd
2
temp = pd.DataFrame({"year":[2011, 2011, 2011, 2011, 2012, 2012, 2012, 2012, 2013, 2013, 2013, 2013, 2013],
3
"price":[190, 270, 330, 225, 138, 92, 76, 190, 110, 140, 160, 180, 170]})
4
5
price year
6
0 190 2011
7
1 270 2011
8
2 330 2011
9
3 225 2011
10
4 138 2012
11
5 92 2012
12
6 76 2012
13
7 190 2012
14
8 110 2013
15
9 140 2013
16
10 160 2013
17
11 180 2013
18
12 170 2013
19
So in this case, I’d want a boxplot for each of 2011, 2012, and 2013 based on their price
column. I’ve looked into DataFrame.groupby
but it produces a different object (a group by object).
Advertisement
Answer
JavaScript
1
2
1
temp.boxplot("price", by="year")
2
is this what you are looking for?