简体   繁体   中英

How do you plot by Groupby?

我想知道如何在“ new_build”组之间绘制条形图,X轴显示城镇,Y轴显示在以下代码中执行的计算所得的百分比值

  df_House.groupby(['new_build', 'town'])['price_paid'].count()/df_House.groupby(['new_build', 'town'])['price_paid'].count().sum()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

data = [['a', 'x', 100], ['b', 'y', 200], ['c', 'z', 300], ['a', 'y', 400], ['b', 'z', 600], ['c', 'x', 100]]
df = pd.DataFrame(data, columns=['new_build', 'town', 'price_paid'])

df_town = df.groupby(['new_build', 'town']).agg({'price_paid': 'sum'})
new_df = df_town.div(state, level='new_build') * 100
new_df.reset_index(inplace=True)

sns.set()
new_df.set_index(['new_build', 'town']).price_paid.plot(kind='bar', stacked=True)
plt.ylabel('% of price_paid')

在此处输入图片说明

Not sure if you need a stacked graph to better represent the data but to keep things less complicated, I have created a bar graph performing the task you requested.

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