本文介绍了 pandas 重新采样到季度,并显示开始和结束月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的df看起来像这样:

My df looks like this:

            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

该指数是按月计算,我希望它是每季度一次:

The index is on a monthly basis and I want it to be quarterly:

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

但是我想显示这样的索引,而不是结束日期.所需的df:

But I would like to show the index like this Start month - End month 2017 instead of the end date. Desired 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

有没有做到这一点的大熊猫方法?我是这样做的,但是它很脏,我敢肯定有更好的方法可以做到(示例中缺少docs中的resample方法...):

Is there a pandas way of doing it? I did it like this but it is quite dirty and I am sure there is a better way to do it (the resample method in docs is lacking in examples...):

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)

推荐答案

使用时段:

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)]

这篇关于 pandas 重新采样到季度,并显示开始和结束月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 15:22
查看更多