滚动配置文件

时间:2018-02-10 07:17:31

标签: python-3.x pandas

我有一个pandas数据框,如下所示:

import pandas as pd
import datetime as dt

df= pd.DataFrame({'date':['2017-12-31','2017-12-31'],'type':['Asset','Asset'],'Amount':[365,45],'Maturity Date':['2019-01-02','2018-07-02']})
df

我想创建一个"滚降"显示前三个月和接下来的五个季度的个人资料。我已经能够使用以下代码执行此操作:

#First Month
df['2018-01-31']=0
df.loc[df['Maturity Date']>'2018-01-31','2018-01-31']=df['Amount']

#Second Month
df['2018-02-28']=0
df.loc[df['Maturity Date']>'2018-02-28','2018-02-28']=df['Amount']

#Third Month
df['2018-03-31']=0
df.loc[df['Maturity Date']>'2018-03-31','2018-03-31']=df['Amount']

#First Quarter
df['2018-06-30']=0
df.loc[df['Maturity Date']>'2018-06-30','2018-06-30']=df['Amount']

#Second Quarter
df['2018-09-30']=0
df.loc[df['Maturity Date']>'2018-09-30','2018-09-30']=df['Amount']

我想知道是否有更有效的方法来实现这一目标?具体来说,我想要一种方法:

  1. 不要手动输入前三个月和后续季度
  2. 避免每个月和每季度重复使用类似的代码

1 个答案:

答案 0 :(得分:1)

您可以使用:

#convert to datetime if necessary
df['Maturity Date'] = pd.to_datetime(df['Maturity Date'])

d = '2018-01-31'
#generate first month and next quarters
c1 = pd.date_range(d, periods=3, freq='M')
c2 =  pd.date_range(c1[-1], periods=3, freq='Q')
#join together
c = c1.union(c2[1:])

#compare column with joined datetimes
m = df['Maturity Date'].values[:, None] > c.values
#get values by boolean mask
b = np.where(m, df['Amount'].values[:, None], 0)
#join output df to original, format columns with strftime
df1 = df.join(pd.DataFrame(b, columns=c.strftime('%Y-%m-%d')))
print (df1)
   Amount Maturity Date        date   type  2018-01-31  2018-02-28  \
0     365    2019-01-02  2017-12-31  Asset         365         365   
1      45    2018-07-02  2017-12-31  Asset          45          45   

   2018-03-31  2018-06-30  2018-09-30  
0         365         365         365  
1          45          45           0