gpt4 book ai didi

python - Pandas 按季度重新采样,并显示开始和结束月份

转载 作者:太空宇宙 更新时间:2023-11-03 14:45:02 32 4
gpt4 key购买 nike

我的 df 看起来像这样:

            Total
language Julia Python R SQLite
date
2015-03-01 NaN NaN 17.0 NaN
2015-04-01 NaN 156.0 189.0 NaN
2015-05-01 13.0 212.0 202.0 NaN

该指数按月计算,我希望按季度计算:

df.resample("Q").sum()

给了我这个:

            Total
language Julia Python R SQLite
date
2015-03-31 NaN NaN 17.0 NaN
2015-06-30 22.0 677.0 594.0 26.0
2015-09-30 37.0 1410.0 1250.0 146.0

但我想显示这样的索引 Start Month - End Month 2017 而不是结束日期。所需的 df:

                Total
language Julia Python R SQLite
Jan - Mar, 2015 NaN NaN 17.0 NaN
Apr - Jun, 2015 22.0 677.0 594.0 26.0
Jul - Sep, 2015 37.0 1410.0 1250.0 146.0

有 Pandas 的方法吗?我这样做了,但它很脏,我确信有更好的方法来做到这一点(文档中的重新采样方法缺乏示例......):

def quarterlyMonthNmaes(x): 
start_date = x.name - pd.offsets.MonthBegin(3)
final_date = str(start_date.strftime('%b')) + " - " + str(x.name.strftime('%b, %Y'))
return final_date
df["Total"].apply(quarterlyMonthNmaes, axis=1)

最佳答案

使用periods :

idx = df.index.to_period('Q')
df.index = ['{0[0]}-{0[1]}'.format(x) for x in zip(idx.asfreq('M', 's').strftime('%b'),
idx.asfreq('M', 'e').strftime('%b %Y'))]
print (df)

Total
language Julia Python R SQLite
Jan-Mar 2015 NaN NaN 17.0 NaN NaN
Apr-Jun 2015 22.0 677.0 594.0 26.0 NaN
Jul-Sep 2015 37.0 1410.0 1250.0 146.0 NaN

或更简单:

idx2 = df.index.strftime('%b %Y')
idx1 = (df.index - pd.offsets.MonthBegin(3)).strftime('%b')
df.index = ['{0[0]}-{0[1]}'.format(x) for x in zip(idx1, idx2)]

关于python - Pandas 按季度重新采样,并显示开始和结束月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46296732/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com