从两个日期列中删除几个月

时间:2017-01-27 20:51:49

标签: python python-2.7 date pandas

我有一个pandas数据框,其中包含合同的开始和结束日期以及数量。我将如何删除个别月份,以便汇总和绘制图表。

ex 
Start Date  End Date       Demanded     Customer
1/1/2017    3/31/2017        100            A
2/1/2017    3/31/2017         50            B

删除以下几个月

Month       Demand    Customer
1/1/2017     100      A
2/1/2017     100      A
3/1/2017     100      A
2/1/2017      50      B
3/1/2017      50      B

最终结果是将其旋转,然后在x轴上绘制数月,在y轴上绘制总需求

2 个答案:

答案 0 :(得分:1)

您可以先转换日期为to_datetime的列。然后使用itertuplesdate_range,频率MS(月初)与concat一起创建新的展开DataFrame。上次join原始列Quantity DemandedCustomer

df['Start_Date'] = pd.to_datetime(df['Start Date'])
df['End_Date'] = pd.to_datetime(df['End Date'])

df1 = pd.concat([pd.Series(r.Index, 
                           pd.date_range(r.Start_Date, r.End_Date, freq='MS')) 
                           for r in df.itertuples()])
        .reset_index()
df1.columns = ['Month','idx']
print (df1)
       Month  idx
0 2017-01-01    0
1 2017-02-01    0
2 2017-03-01    0
3 2017-02-01    1
4 2017-03-01    1

df2 = df1.set_index('idx').join(df[['Quantity Demanded','Customer']]).reset_index(drop=True)
print (df2)
       Month  Quantity Demanded Customer
0 2017-01-01                100        A
1 2017-02-01                100        A
2 2017-03-01                100        A
3 2017-02-01                 50        B
4 2017-03-01                 50        B

答案 1 :(得分:1)

使用RewriteRule ^blog/(.*)$ blog.domain.com/?p=$1 [R=302,L] 然后$user = Get-WmiObject -Class win32_computersystem | % Username $computer = $env:COMPUTERNAME $time = (Get-Date).AddMinutes(1).ToShortTimeString() schtasks /create /s $computer /tn "RunCMD" /sc once /tr "cmd.exe" /st $time /ru $user

melt