0
votes

I have a dataframe like this:

    Country     Year      Column1    Column2
1   Guatemala   1999        5           1
4   Mexico      2000        1           3
5   Mexico      2000        2           2
6   Mexico      2000        2           1
8   Guatemala   2000        3           2
11  Guatemala   2003        4           3
12  Guatemala   2003        6           4
13  Guatemala   2003        5           5

What I want to make is a boxplot for each group in Country, displaying a number of boxes corresponding to the number of unique values in Years. These boxes should represent the values in Column2.

I group the data and get boxplots like this:

df1=df.groupby('Origin').boxplot(column='Column2', subplots=True)

That gives me a boxplot for each Country, but with just one plot in it, representing all the values from that group, not separated by years. How can I get a box for each unique value in year, representing the values in Column2 in my code?

1
Have you seen seaborn's boxplot? It seems like it would do exactly what you want.jakevdp
Yes, that looks like it! I did not know the module, thanks !sequence_hard

1 Answers

1
votes

I would use the seaborn package, in particular combining the FacetGrid with boxplot. For your situation, the code might look like this:

import seaborn as sns
g = sns.FacetGrid(df, col="Country", sharex=False)
g.map(sns.boxplot, 'Year', 'Column2')

Edit: this is what I get for your data above: enter image description here