One year of sample data:
import pandas as pd
import numpy.random as rnd
import seaborn as sns
n = 365
df = pd.DataFrame(data = {"A":rnd.randn(n), "B":rnd.randn(n)+1},
index=pd.date_range(start="2017-01-01", periods=n, freq="D"))
I want to put this data side by side, grouped by month (that is, two blocks per month, one for A
and one for B
).
For one column it sns.boxplot(df.index.month, df["A"])
works fine. However, it sns.boxplot(df.index.month, df[["A", "B"]])
throws an error ( ValueError: cannot copy sequence with size 2 to array axis with dimension 365
). Merging data by index ( pd.melt(df, id_vars=df.index, value_vars=["A", "B"], var_name="column")
) to use the seaborn property hue
as a workaround also does not work ( TypeError: unhashable type: 'DatetimeIndex'
).
(Itโs not necessary to use seaborn for the solution, if it is easier to use a simple matplotlib.)
Edit
, , . , , DataFrame , . , / , , !
df_stacked = df.stack().reset_index()
df_stacked.columns = ["date", "vars", "vals"]
df_stacked.index = df_stacked["date"]
sns.boxplot(x=df_stacked.index.month, y="vals", hue="vars", data=df_stacked)
: