pandas 使用重采样将季度目标转换为月度目标,但它只提供了1月到10月的目标,最后两个月的目标缺失

k2fxgqgv  于 2023-02-02  发布在  其他
关注(0)|答案(1)|浏览(127)

我的目标是将季度目标转换为月度目标。
下面是我的代码,我指定了一个销售人员每季度的会议次数。然后使用重采样方法,我将季度目标转换为月度目标。但是,输出只给了我一月到十月之间的目标。

import pandas as pd

# INPUT
# create quarterly meeting targets per sales person
quarters =  ['2023-Q1','2023-Q2','2023-Q3','2023-Q4']
anne =      [40,40,40,40]
martijn =   [40,40,40,40]
kevin =     [40,40,40,40]
danny =     [40,40,40,40]
rick =      [40,40,40,40]
fiona =     [35,35,35,35]
df = pd.DataFrame({'quarter': quarters, 'anne': anne, 'martijn': martijn, 'danny': danny, 'kevin': kevin, 'rick': rick, 'fiona': fiona})

df_melted = df.melt(id_vars=['quarter'], var_name='sales person', value_name='meeting target') # flatten table
df_melted['meeting target'] = df_melted['meeting target'].astype(float)/3 # convert meeting target column to float
df_melted['quarter'] = pd.to_datetime(df_melted['quarter']) #convert quarter column to date type

df_melted = df_melted.set_index('quarter').groupby('sales person').resample('M')['meeting target'].ffill().round(2)
df_melted = df_melted.reset_index()
df_melted

下面是其中一个销售人员的输出,如您所见,输出只持续到2023-10-31,而不是2023-12-31。
output
有人能告诉我我错过了什么吗?

wgmfuz8q

wgmfuz8q1#

这里有一个变通方法,只是玩不同的选项。显然转换到periodIndex而不是datetime的作品,但我不知道为什么。我同意你的看法,这似乎是奇怪的行为,没有得到所有的几个月后,重新采样。

import pandas as pd

# INPUT
# create quarterly meeting targets per sales person
quarters =  ['2023-Q1','2023-Q2','2023-Q3','2023-Q4']
anne =      [30,60,90,120]
martijn =   [33,63,93,123]
df = pd.DataFrame({'quarter': quarters, 'anne': anne, 'martijn': martijn})

df_melted = df.melt(id_vars=['quarter'], var_name='sales person', value_name='meeting target') # flatten table
df_melted['meeting target'] = df_melted['meeting target'].astype(float)/3 # convert meeting target column to float

#NOTE: this is the one difference, making a PeriodIndex instead of a datetime index
#df_melted['month'] = pd.to_datetime(df_melted['quarter']) #convert quarter column to date type
df_melted['month'] = pd.PeriodIndex(df_melted['quarter'], freq='Q')

out_df = (
    df_melted.set_index('month')
        .groupby('sales person')
        .resample('M', convention='start')['meeting target'] #setting convention='start' is also req'd
        .ffill()
        .round(2)
        .reset_index()
)
out_df

相关问题