【问题标题】:How do I convert monthly data into quarterly如何将月度数据转换为季度
【发布时间】:2019-12-06 00:57:45
【问题描述】:

我有一个数据集,需要将其从月度数据转换为季度数据。

这是我数据的前五行。

                   Measure Name             Year    Month   Value
0   Revenue from Sale of Recycled Materials 2007    Jan $1,757,000
1   Revenue from Sale of Recycled Materials 2007    Feb $2,052,000
2   Revenue from Sale of Recycled Materials 2007    Mar $2,747,000
3   Revenue from Sale of Recycled Materials 2007    Apr $2,308,000
4   Revenue from Sale of Recycled Materials 2007    May $2,289,000

我不知道从哪里开始将每月转换为每季度。

Jan-Mar will be Q1
April-June will be Q2
July-September will be Q3
October-December will be Q4. 

我考虑创建一个 Chort 组分析,但由于我的时间数据分为两列,我不知道从哪里开始。

这是我目前的代码

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
%matplotlib inline
from pandas_datareader import data as pdr
from pandas.plotting import autocorrelation_plot
import seaborn as sns
from sklearn.metrics import accuracy_score, classification_report
plt.style.use(style='ggplot')

from datetime import datetime
from datetime import timedelta

recycle=pd.read_csv('Pathway link',
                       sep=',',)

我期待2007 Jan-Mar 成为Q1 等等......

【问题讨论】:

    标签: python pandas time-series


    【解决方案1】:

    您可以通过多种方式解决这个问题,第一种是将月份视为数字,并应用一些逻辑测试语句。例如if month <= 3 THEN QTR = 'Q1'

    但您似乎处理的是字符串而不是日期时间对象,因此我们可以尝试将月份传递给 dict 并将 Quarter 作为值对。当然,这取决于您的月份是否与密钥相同。

    months = { 'Jan' : 'Q1',
          'Feb' : 'Q1',
          'Mar' : 'Q1',
          'Apr' : 'Q2',
          'May' : 'Q2',
          'Jun' : 'Q2',
          'Jul' : 'Q3',
          'Aug' : 'Q3',
          'Sep' : 'Q3',
          'Oct' : 'Q4',
          'Nov' : 'Q4',
          'Dec' : 'Q4' }
    
    df['Qtr'] = df['Month'].map(months)
    print(df)
        Measure Name    Year    Month   Value   QTR
    0   Revenue from Sale of Recycled Materials 2007    Jan $1,757,000  Q1
    1   Revenue from Sale of Recycled Materials 2007    Feb $2,052,000  Q1
    2   Revenue from Sale of Recycled Materials 2007    Mar $2,747,000  Q1
    3   Revenue from Sale of Recycled Materials 2007    Apr $2,308,000  Q2
    4   Revenue from Sale of Recycled Materials 2007    May $2,289,000  Q2
    

    然后您可以通过聚合应用您的组:

    df.groupby('QTR')['Value'].sum() 
    #you'll need to convert your value into a number if its an object.
    QTR
    Q1    6556000
    Q2    4597000
    Name: Value, dtype: int64
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-28
      • 2021-10-14
      • 2017-03-22
      • 1970-01-01
      • 2022-11-10
      • 2021-03-08
      • 2023-03-31
      • 2022-01-01
      相关资源
      最近更新 更多