首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Python中的Pandas从具有季度数据的行创建单列月度值?

如何使用Python中的Pandas从具有季度数据的行创建单列月度值?
EN

Stack Overflow用户
提问于 2019-04-13 03:26:34
回答 1查看 100关注 0票数 4

我的数据框包含季度数据,对于某些公司,还包含月度数据。

代码语言:javascript
复制
import pandas as pd
df = pd.DataFrame({'quarter': ['2010-1', '2010-2', '2010-3','2010-4', '2011-1'],
                  'volume_quarter': [450, 450, 450, 450, 450],
                  'volume_month_1': [150, 150, 150, 150, 150],
                  'volume_month_2': [160, 160, 160, 160, 160],
                  'volume_month_3': [140, 140, 140, 140, 140]})
df

提供:

代码语言:javascript
复制
quarter volume_quarter  volume_month_1  volume_month_2  volume_month_3
2010-1  450               150            160               140
2010-2  450               150            160               140
2010-3  450               150            160               140
2010-4  450               150            160               140
2011-1  450               150            160               140

使用以下代码:

pd.melt(df, id_vars = ['quarter'], value_vars=['volume_month_1', "volume_month_2", "volume_month_3"])

我得到了:

代码语言:javascript
复制
    quarter variable    value
0   2010-1  volume_month_1  150
1   2010-2  volume_month_1  150
2   2010-3  volume_month_1  150
3   2010-4  volume_month_1  150
4   2011-1  volume_month_1  150
5   2010-1  volume_month_2  160
6   2010-2  volume_month_2  160
7   2010-3  volume_month_2  160
8   2010-4  volume_month_2  160
9   2011-1  volume_month_2  160
10  2010-1  volume_month_3  140
11  2010-2  volume_month_3  140
12  2010-3  volume_month_3  140
13  2010-4  volume_month_3  140
14  2011-1  volume_month_3  140

相反,我正在尝试实现以下目标:

代码语言:javascript
复制
    quarter variable        value
0   2010-1  volume_month_1  150
1   2010-1  volume_month_2  160
2   2010-1  volume_month_3  140
3   2010-2  volume_month_1  150
4   2010-2  volume_month_2  160
5   2010-2  volume_month_3  140
6   2010-3  volume_month_1  150
7   2010-3  volume_month_2  160
8   2010-3  volume_month_3  140
9   2010-4  volume_month_1  150
10  2010-4  volume_month_2  160
11  2010-4  volume_month_3  140
12  2011-1  volume_month_1  150
13  2011-1  volume_month_2  160
14  2011-1  volume_month_3  140

我想要实现这一点,这样我就可以在montly值上运行Arima模型。

万分感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-13 18:15:35

您只错过了排序,下面这行代码:

代码语言:javascript
复制
df = (
    pd.melt(
        df,
        id_vars=["quarter"],
        value_vars=["volume_month_1", "volume_month_2", "volume_month_3"],
    )
    .sort_values(by="quarter")
    .reset_index(drop=True)
)

根据您的需要返回:

代码语言:javascript
复制
   quarter        variable  value
0   2010-1  volume_month_1    150
1   2010-1  volume_month_2    160
2   2010-1  volume_month_3    140
3   2010-2  volume_month_1    150
4   2010-2  volume_month_2    160
5   2010-2  volume_month_3    140
6   2010-3  volume_month_1    150
7   2010-3  volume_month_2    160
8   2010-3  volume_month_3    140
9   2010-4  volume_month_1    150
10  2010-4  volume_month_2    160
11  2010-4  volume_month_3    140
12  2011-1  volume_month_1    150
13  2011-1  volume_month_2    160
14  2011-1  volume_month_3    140
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55658276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档