我有一个数据集,需要将其从月度数据转换为季度数据。
这是我数据的前五行。
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
,依此类推。...
答案 0 :(得分:0)
您可以通过多种方式解决此问题,首先是将Months视为数字,并应用一些逻辑测试语句。例如if month <= 3 THEN QTR = 'Q1'
但是似乎您正在处理字符串而不是datetime对象,因此我们可以尝试将月份传递到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