简体   繁体   中英

pandas: create barplot from multiindex in columns

I would like to use seaborn (matplotlib would be good, too) to create a barplot from my DataFrame.

But judging from the docs, the barplot function expects a list of values that looks like this: 在此处输入图片说明

Then you can plot it with:

tips = sns.load_dataset("tips")
sns.barplot(x="day", y="total_bill", hue="sex", data=tips)

My data looks different, I have created a multi_index in the columns. Since I can't publish my original data, here is a mockup on how it would look for the tips dataset:

在此处输入图片说明

And here is the code that creates the above dataframe:

index_tuples=[]

for sex in ["Male", "Female"]:
    for day in ["Sun", "Mon"]:
        index_tuples.append([sex, day])

index = pd.MultiIndex.from_tuples(index_tuples, names=["sex", "day"])

dataframe = pd.DataFrame(columns = index)

total_bill = {"Male":{"Sun":5, "Mon":3},"Female":{"Sun":10, "Mon":5}}
dataframe = dataframe.append(pd.DataFrame.from_dict(total_bill).unstack().rename('total_bill'))

Now, my question is: How can I create a barplot from this multiindex ? The solution should group the bars correctly, as does the hue argument of seaborn. Simply getting the data as an array and passing it to matplotlib doesn't work.

My solution so far is to converting the multiindex into columns by repeatedly stacking the DataFrame. Like this:

stacked_frame = dataframe.stack().stack().to_frame().reset_index()

It results in the data layout expected by seaborn:

在此处输入图片说明

And you can plot it with

sns.barplot(x="day", y=0, hue="sex", data=stacked_frame)
plt.show()

在此处输入图片说明

Can I create a barplot directly from the multiindex ?

Is this what you are looking for?

idx = pd.MultiIndex.from_product([['M', 'F'], ['Mo', 'Tu', 'We']], names=['sex', 'day'])
df = pd.DataFrame(np.random.randint(2, 10, size=(idx.size, 1)), index=idx, columns=['total bill'])
df.unstack(level=0)['total bill'].plot(
    kind='bar'
)
plt.ylabel('total bill');

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM