2

I've created a pandas dataframe of PM2.5 data and want to create individual boxplots for each site with box and whisker plots for each year within this (with the top and bottom 10th percentiles). How do I go about this?

Date            Aberdeen  Auchencorth Moss  Belfast Centre  Birmingham Tyburn  

2000-01-01        NaN               NaN             NaN                NaN   
2000-01-02        NaN               NaN             NaN                NaN   
2000-01-03        NaN               NaN             NaN                NaN   
2000-01-04        NaN               NaN             NaN                NaN   
2000-01-05        NaN               NaN             NaN                NaN   
2000-01-06        NaN               NaN             NaN                NaN   
2000-01-07        NaN               NaN             NaN                NaN   
2000-01-08        NaN               NaN             NaN                NaN   
2000-01-09        NaN               NaN             NaN                NaN   
2000-01-10        NaN               NaN             NaN                NaN   
2000-01-11        NaN               NaN             NaN                NaN   
2000-01-12        NaN               NaN             NaN                NaN   
2000-01-13        NaN               NaN             NaN                NaN   
2000-01-14        NaN               NaN             NaN                NaN   
2000-01-15        NaN               NaN             NaN                NaN   
2000-01-16        NaN               NaN             NaN                NaN   
2000-01-17        NaN               NaN             NaN                NaN   
2000-01-18        NaN               NaN             NaN                NaN   
2000-01-19        NaN               NaN             NaN                NaN   
2000-01-20        NaN               NaN             NaN                NaN   
2000-01-21        NaN               NaN             NaN                NaN   
2000-01-22        NaN               NaN             NaN                NaN   
2000-01-23        NaN               NaN             NaN                NaN   
2000-01-24        NaN               NaN             NaN                NaN   
2000-01-25        NaN               NaN             NaN                NaN   
2000-01-26        NaN               NaN             NaN                NaN   
2000-01-27        NaN               NaN             NaN                NaN   
2000-01-28        NaN               NaN             NaN                NaN   
2000-01-29        NaN               NaN             NaN                NaN   
2000-01-30        NaN               NaN             NaN                NaN   
              ...               ...             ...                ...   
2017-04-02        3.0               4.0             7.0               10.0   
2017-04-03        5.0               4.0             9.0               14.0   
2017-04-04        3.0               5.0             8.0                9.0   
2017-04-05        7.0               5.0             7.0                7.0   
2017-04-06        3.0               3.0             7.0               10.0   
2017-04-07        3.0               3.0            11.0               14.0   
2017-04-08       11.0              12.0            20.0               26.0   
2017-04-09       11.0              15.0            17.0               25.0   
2017-04-10        3.0               4.0             8.0                5.0   
2017-04-11        1.0               6.0             9.0                7.0   
2017-04-12        2.0               4.0             5.0                6.0   
2017-04-13        2.0               3.0             6.0                6.0   
2017-04-14        2.0               3.0             6.0                6.0   
2017-04-15        3.0               3.0             6.0                6.0   
2017-04-16        3.0               3.0             5.0                5.0   
2017-04-17        4.0               3.0             7.0               11.0   
2017-04-18        4.0               3.0             7.0                7.0   
2017-04-19        6.0               4.0            11.0               13.0   
2017-04-20        3.0               4.0            12.0               12.0   
2017-04-21        3.0               4.0            11.0               11.0   
2017-04-22        3.0               4.0             9.0                8.0   
2017-04-23        3.0               4.0             6.0                9.0   
2017-04-24        3.0               2.0             4.0                6.0   
2017-04-25        3.0               3.0             6.0                5.0   
2017-04-26        3.0               3.0             6.0                6.0   
2017-04-27        3.0               2.0             6.0                8.0   
2017-04-28        NaN               3.0             8.0                8.0   
2017-04-29        NaN               6.0             7.0                9.0   
2017-04-30        NaN              17.0            20.0               19.0   
2017-05-01       19.0              18.0            20.0                8.0

3 Answers 3

2

Here's a go.

import matplotlib.pyplot as plt
import pandas as pd

# copy your sample
df = pd.read_clipboard(header=0, index_col='Date').fillna(0)

# remove row with '...'
df = df[df.Moss != '...'].astype(float)

# set index to datetime index
df.index = pd.DatetimeIndex(df.index)

# groupby year
grouped = dict(list(df.groupby(date_index.year)))

# set up a figure with 1 row, 2 colums
fig, ax = plt.subplots(1, 2,
                       sharey=True, 
                       figsize=(8, 6), 
                       tight_layout=True)

# iterate through our grouped and plot 
for i, (k, v) in enumerate(grouped.items()):
    v.boxplot(ax=ax[i],
              rot=90, 
              figsize=(3, 3)).set(title=f'{k}')

plt.show()

enter image description here

Year 2000 has no data points, but you can see the gist of it.

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

Comments

1

You could try using matplotlib

import matplotlib.pyplot as plt
plt.boxplot(your_df['Aberdeen'].values)
plt.show()

If you are using jupyter use %matplotlib inline

Comments

0
import pandas as pd
df = pd.DataFrame({'Date': ['2000-10-10', '2000-09-20', '2001-09-23', '2001-09-10', '2001-09-02', '2002-04-29',],
          'Aberdeen': [9,5,1, 2, 6, 1]})

df['Year'] = df['Date'].str.split('-')
df['Year'] = df['Year'].apply(lambda x : x[0])
sns.boxplot(df['Year'], df['Aberdeen'])

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.