使用情节计算每月平均价格和情节

发布于 2025-02-06 13:58:45 字数 842 浏览 1 评论 0原文

我有一个数据框架,该数据框架在2021年10月至今天的每日时间内都包含两个地区的售价。

我想找到每月的平均房价,并使用Plotly绘制图表,以查看每个区域的价格是否下跌或上涨。

示例dataframe:

DateSold            Price    Area
12/10/2021 00:00    300000  Area A
17/10/2021 00:00    350000  Area B
18/10/2021 00:00    400000  Area B
11/12/2021 00:00    412000  Area A
17/12/2021 00:00    315000  Area A
08/01/2022 00:00    385000  Area A
09/01/2022 00:00    445000  Area A
15/01/2022 00:00    309000  Area B
15/01/2022 00:00    350000  Area B

我试图按月频率将dateTime列转换为周期索引,然后使用groupby.mean:emean:

df2.groupby(pd.PeriodIndex(df2['Datesold'], freq="M"))['Price'].mean()

有人可以指向我如何达到我如何达到平均房价的正确方向每月并使用情节绘制?

我正在使用以下代码进行绘图

fig = px.line(df, x = df['DateSold'], y = df['Price'], title='Average sold house price

I have a Dataframe which contains sold house prices in two areas over a daily period from Oct 2021 till today.

I want to find the average house price per month and plot a graph using plotly to see if prices are going down or up in each area.

Sample DataFrame:

DateSold            Price    Area
12/10/2021 00:00    300000  Area A
17/10/2021 00:00    350000  Area B
18/10/2021 00:00    400000  Area B
11/12/2021 00:00    412000  Area A
17/12/2021 00:00    315000  Area A
08/01/2022 00:00    385000  Area A
09/01/2022 00:00    445000  Area A
15/01/2022 00:00    309000  Area B
15/01/2022 00:00    350000  Area B

I tried to convert the datetime column into a PeriodIndex on monthly frequency, then take the mean using GroupBy.mean:

df2.groupby(pd.PeriodIndex(df2['Datesold'], freq="M"))['Price'].mean()

Can someone point me in the right direction in how I can achieve the average house price per month and plot that using plotly?

I was using the below code for plotly

fig = px.line(df, x = df['DateSold'], y = df['Price'], title='Average sold house price

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池木 2025-02-13 13:58:45

您的DateTime列没有频率或周期性。即使是这种情况,您也无需转换为ofientIndex。您可以使用datesold列的月份/月_名称进行分组,如下所示:

# convert object to datetime
df.DateSold = pd.to_datetime(df.DateSold, format="%d/%m/%Y %H:%M")

# compute the monthly averages
df_avg = df.groupby(df.DateSold.dt.month_name(), as_index=True)['Price'].mean()
DateSold
December    363500.0
January     372250.0
October     350000.0

然后,您可以用plotly绘制结果

fig = px.bar(df_avg, x = df_avg.index, y = df_avg, title='Average sold house price')
fig

Your DateTime column does not have a frequency or periodicity. Even if it were the case, you wouldn't need to convert to PeriodIndex. You can group by using the month/month_name of the DateSold column as follows:

# convert object to datetime
df.DateSold = pd.to_datetime(df.DateSold, format="%d/%m/%Y %H:%M")

# compute the monthly averages
df_avg = df.groupby(df.DateSold.dt.month_name(), as_index=True)['Price'].mean()
DateSold
December    363500.0
January     372250.0
October     350000.0

You can then plot the result with plotly

fig = px.bar(df_avg, x = df_avg.index, y = df_avg, title='Average sold house price')
fig
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文