2016-08-28 157 views
0

我正在查詢我的數據庫以顯示過去一週的記錄。然後我彙總數據並將其轉換爲Python和熊貓到DataFrame中。 在此表中,我試圖說明過去7周內每天發生的情況,但是在某些日子裏沒有發生任何事件。在這些情況下,日期完全缺失。我正在尋找一種方法來追加不存在的日期(但它們是查詢中指定的日期範圍的一部分),這樣我就可以填充任何希望存在其他缺失列的值。日期範圍填寫日期範圍和fillna

在一些試驗中,我將數據設置爲熊貓數據框,其中日期是索引,而其他日期是列。我最好將日期作爲頂部索引 - 所以按名稱,堆棧購買和send_back分組,並且日期是'列'。

以下是我找的數據幀現在怎麼看一個例子,:在查詢設置

日期 - 2016年8月1日 - 2016年8月8日。數據框看起來鏈接纔可這樣:

 | dates  | name  | purchase | send_back 
    0 01.08.2016 Michael  120   0 
    1 02.08.2016 Sarah  100   40 
    2 04.08.2016 Sarah  55   0 
    3 05.08.2016 Michael  80   20 
    4 07.08.2016 Sarah  130   0 

後:

 | dates  | name  | purchase | send_back 
    0 01.08.2016 Michael  120   0 
    1 02.08.2016 Sarah  100   40 
    2 03.08.2016 -   0   0 
    3 04.08.2016 Sarah  55   0 
    4 05.08.2016 Michael  80   20 
    5 06.08.2016 -   0   0 
    6 07.08.2016 Sarah  130   0 
    7 08.08.2016 Sarah  0   35 
    8 08.08.2016 Michael  20   0 

打印如下:

df.index 

給出:

'Index([ u'dates',u'name',u'purchase',u'send_back'], 
     dtype='object') 

RangeIndex(start=0, stop=1, step=1)' 

我明白任何指導。

回答

1

假設你有以下DF:

In [93]: df 
Out[93]: 
       name purchase send_back 
dates 
2016-08-01 Michael  120   0 
2016-08-02 Sarah  100   40 
2016-08-04 Sarah  55   0 
2016-08-05 Michael  80   20 
2016-08-07 Sarah  130   0 

你可以重新取樣並替換:

In [94]: df.resample('D').first().replace({'name':{np.nan:'-'}}).fillna(0) 
Out[94]: 
       name purchase send_back 
dates 
2016-08-01 Michael  120.0  0.0 
2016-08-02 Sarah  100.0  40.0 
2016-08-03  -  0.0  0.0 
2016-08-04 Sarah  55.0  0.0 
2016-08-05 Michael  80.0  20.0 
2016-08-06  -  0.0  0.0 
2016-08-07 Sarah  130.0  0.0 
+0

感謝的方式。我嘗試過,但我得到一個錯誤KeyError'日期'。所以儘管它沒有識別索引名稱(參見上文,我在主要問題中添加了信息)。我在熊貓和python領域有點新手,所以我真的不確定問題在哪裏。 – OAK

+2

@OAK MaxU將數據框的索引設置爲'date'列。他從你發佈的文本中推斷出它的存在。我們都在你的數據框中看到'date'。您得到的錯誤必須來自「date」已在索引中的事實。嘗試從MaxU的代碼中去除'.set_index('dates')'。 – piRSquared

+0

@piRSquared,謝謝! – MaxU

1

你的指數爲object型的,你必須將其轉換爲datetime格式。

# Converting the object date to datetime.date 
df['dates'] = df['dates'].apply(lambda x: datetime.strptime(x, "%d.%m.%Y")) 

# Setting the index column 
df.set_index(['dates'], inplace=True) 

# Choosing a date range extending from first date to the last date with a set frequency 
new_index = pd.date_range(start=df.index[0], end=df.index[-1], freq='D') 
new_index.name = df.index.name 

# Setting the new index 
df = df.reindex(new_index) 

# Making the required modifications 
df.ix[:,0], df.ix[:,1:] = df.ix[:,0].fillna('-'), df.ix[:,1:].fillna(0) 

print (df) 

       name purchase send_back 
dates         
2016-08-01 Michael  120.0  0.0 
2016-08-02 Sarah  100.0  40.0 
2016-08-03  -  0.0  0.0 
2016-08-04 Sarah  55.0  0.0 
2016-08-05 Michael  80.0  20.0 
2016-08-06  -  0.0  0.0 
2016-08-07 Sarah  130.0  0.0 

讓我們假設你有一個單日的數據(如在評論部分提到)願與您填寫在一週中的其它日期與空值:

數據設置:

df = pd.DataFrame({'dates':['01.08.2016'], 'name':['Michael'], 
        'purchase':[120], 'send_back':[0]}) 
print (df) 

     dates  name purchase send_back 
0 01.08.2016 Michael  120   0 

操作:

df['dates'] = df['dates'].apply(lambda x: datetime.strptime(x, "%d.%m.%Y")) 
df.set_index(['dates'], inplace=True) 

# Setting periods as 7 to account for the end of the week 
new_index = pd.date_range(start=df.index[0], periods=7, freq='D') 
new_index.name = df.index.name 

# Setting the new index 
df = df.reindex(new_index) 
print (df) 

       name purchase send_back 
dates         
2016-08-01 Michael  120.0  0.0 
2016-08-02  NaN  NaN  NaN 
2016-08-03  NaN  NaN  NaN 
2016-08-04  NaN  NaN  NaN 
2016-08-05  NaN  NaN  NaN 
2016-08-06  NaN  NaN  NaN 
2016-08-07  NaN  NaN  NaN 

櫃面你想用0來填補空值,你可以這樣做:

df.fillna(0, inplace=True) 
print (df) 
       name purchase send_back 
dates         
2016-08-01 Michael  120.0  0.0 
2016-08-02  0  0.0  0.0 
2016-08-03  0  0.0  0.0 
2016-08-04  0  0.0  0.0 
2016-08-05  0  0.0  0.0 
2016-08-06  0  0.0  0.0 
2016-08-07  0  0.0  0.0 
+0

@OAK,你能找到解決這兩個問題的解決方案嗎? –

+0

感謝您再次聯繫。很不幸的是,不行。我會稍後調整原始帖子,以顯示更多細節。 – OAK

+0

我沒有任何運氣就嘗試過你的解決方案以及MaxU。我很抱歉給你之前不準確的信息。使用這兩種解決方案,我遇到的主要問題是,當我完成fillna(0)時,無論我的'日期'是索引還是列,所有數據條目(值)都將更改爲'0'。因此,例如,我能夠將日期範圍添加爲索引,然後填充(fillna = 0)將獲得行和列零的所有值。 – OAK