【问题标题】:Python stats and visualizationPython 统计和可视化
【发布时间】:2020-03-21 11:42:50
【问题描述】:

我是 Python 新手,目前正在研究一组来自 redfinn 的房地产数据。

目前我的数据是这样的:

数据集中有许多不同的社区。我想 到:

  1. 获取每月的平均房屋销售量(日期字段已从 屏幕截图)每个社区
  2. 仅使用我希望使用的社区(大约 4).

非常感谢任何帮助。

【问题讨论】:

  • 你试过什么?展示您的努力以及您遇到的问题
  • 你能告诉我你尝试了什么吗?
  • 嗨,我已经尝试过这段代码,并成功获得了每个社区的平均销售价格。但是,我无法通过“period_end”(即日期)对此进行细分,我想知道每个月按社区出售的平均房屋价格。 mean_number_of_homes_sold = data[['neighborhood','median_sale_price']].groupby(['neighborhood']).mean() mean_number_of_homes_sold我在上面代码的不同位置放置了'period_end',但没有成功。

标签: python pandas machine-learning statistics visualization


【解决方案1】:

据我了解,您有不同的每月售出房屋价值,您想取其平均值。如果是这样,请尝试以下代码(改为提供您的数据):

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline

data = pd.DataFrame({'neighborhood':['n1','n1','n2','n3','n3','n4','n5'],'homes_sold per month':[5,7,2,6,4,1,5],'something_else':[5,3,3,5,5,5,5]})
neighborhoods_to_plot = ['n1','n2','n4','n5'] #provide here a list you want to plot
plot = pd.DataFrame()
for n in neighborhoods_to_plot:
    plot.at[n,'homes_sold per month'] = data.loc[data['neighborhood']==n]['homes_sold per month'].mean()
plot.index.name = 'neighborhood'
plt.figure(figsize=(4,3),dpi=300,tight_layout=True)
sns.barplot(x=plot.index,y=plot['homes_sold per month'],data=plot)
plt.savefig('graph.png', bbox_inches='tight')

Plot

【讨论】:

    【解决方案2】:

    好的,所以我假设您正在使用 Pandas 和 Matplotlib 来处理这些数据。然后,为了获得每月平均售出房屋数量,您只需:

    import pandas as pd
    mean_number_of_homes_sold = data[['neighborhood','homes_sold']].groupby['neighborhood'].agg('mean')
    

    为了获得仅与您想要的社区一起绘制的信息,您需要这样的东西

    import pandas as pd
    import matplotlib.pyplot as plt
    #fill this list with strings representing the names of the data you need plotted
    neighborhoods_to_plot = ['Albany Park', 'Tinley Park']
    data_to_graph = data[data.neighborhood.isin(neighborhoods_to_plot)]
    fig, ax = plt.subplots()
    data_to_graph.plot(kind='scatter', x='avg_sale_to_list', y ='inventory_mom')
    ax.set(title='Relationship between time to sale from listing and inventory momentum for selected neighborhoods')
    fig.savefig('neighborhood.png', transparent=False, dpi=300, bbox_inches="tight")
    

    您显然可以更改要绘制的数据或图表的类型,但这应该会给您一个不错的起点。

    【讨论】:

      猜你喜欢
      • 2019-04-10
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 2019-12-24
      相关资源
      最近更新 更多