Pandas Reindex填补缺失的日期或更好的填充方法?

时间:2017-08-04 11:47:12

标签: python python-3.x pandas pandas-groupby

我的数据是工厂缺勤记录。有些日子没有缺席,所以当天没有记录数据或日期。然而,如果显示的其他示例变得毛茸茸,那么在任何一天,由于各种原因可能会有几次缺席。数据中的日期与记录的比率并不总是1比1。

我希望的结果是这样的:

(index)    Shift        Description     Instances (SUM)
01-01-14   2nd Baker    Discipline      0
01-01-14   2nd Baker    Vacation        0
01-01-14   1st Cooks    Discipline      0
01-01-14   1st Cooks    Vacation        0
01-02-14   2nd Baker    Discipline      4
01-02-14   2nd Baker    Vacation        3
01-02-14   1st Cooks    Discipline      3
01-02-14   1st Cooks    Vacation        3

等等。这个想法是所有轮班,描述将包含该时间段内所有日期的值(在此示例2014年1月1日 - 2014年12月31日)

我已经阅读了几个例子,而我最接近的就是here

ts = pd.read_csv('Absentee_Data_2.csv'
                , encoding = 'utf-8'
                ,parse_dates=[3]
                ,index_col=3
                ,dayfirst=True
                )

idx =  pd.date_range('01.01.2009', '12.31.2017')

ts.index = pd.DatetimeIndex(ts.index)
# ts = ts.reindex(idx, fill_value='NaN')
df = pd.DataFrame(index = idx)
df1 = df.join(ts, how='left')

但是,当我取消注释ts = ts.reindex(idx, fill_value='NaN')时,我会收到错误消息。我已经尝试了至少10种其他方法来完成我想要做的事情所以我不是百分之百确定这是正确的道路,但它似乎让我最接近任何进展。

以下是一些示例数据:

Description Unexcused   Instances   Date        Shift
Discipline  FALSE              1    Jan 2 2014  2nd Baker
Vacation    TRUE               2    Jan 2 2014  1st Cooks
Discipline  FALSE              3    Jan 2 2014  2nd Baker
Vacation    TRUE               1    Jan 2 2014  1st Cooks
Discipline  FALSE              2    Apr 8 2014  2nd Baker
Vacation    TRUE               3    Apr 8 2014  1st Cooks
Discipline  FALSE              1    Jun 1 2014  2nd Baker
Vacation    TRUE               2    Jun 1 2014  1st Cooks
Discipline  FALSE              3    Jun 1 2014  2nd Baker
Vacation    TRUE               1    Jun 1 2014  1st Cooks
Vacation    TRUE               2    Jul 5 2014  1st Cooks
Discipline  FALSE              3    Jul 5 2014  2nd Baker
Vacation    TRUE               2    Dec 3 2014  1st Cooks

提前感谢您的帮助,我是新手,2天没有太大进展。我非常感谢这里的人们如何帮助解答,但最重要的是指导解决方案的工作原理。像我这样的新手非常感谢分享的智慧。

2 个答案:

答案 0 :(得分:3)

我认为你在使用datetime时遇到了问题,这种方法对我有用

ts.set_index(['Date'],inplace=True)
ts.index = pd.to_datetime(ts.index,format='%b %d %Y')
d2 = pd.DataFrame(index=pd.date_range('2014-01-01','2014-12-31'))

print ts.join(d2,how='right')

答案 1 :(得分:1)

实际上你非常接近你想要的东西(假设我理解你正在寻找的输出)。请参阅上面代码中我添加的内容:

import pandas as pd

ts = pd.read_csv('Absentee_Data_2.csv', encoding = 'utf-8',parse_dates=[3],index_col=3,dayfirst=True, sep=",")

idx =  pd.date_range('01.01.2009', '12.31.2017')

ts.index = pd.DatetimeIndex(ts.index)
#ts = ts.reindex(idx, fill_value='NaN')
df = pd.DataFrame(index = idx)
df1 = df.join(ts, how='left')
df2 = df1.copy()
df3 = df1.copy()
df4 = df1.copy()
dict1 = {'Description': 'Discipline', 'Instances': 0, 'Shift': '1st Cooks'}
df1 = df1.fillna(dict1)
dict1["Description"] = "Vacation"
df2 = df2.fillna(dict1)
dict1["Shift"] = "2nd Baker"
df3 = df3.fillna(dict1)
dict1["Description"] = "Discipline"
df4 = df4.fillna(dict1)
df_with_duplicates = pd.concat([df1,df2,df3,df4])
final_res = df_with_duplicates.reset_index().drop_duplicates(subset=["index"] + list(dict1.keys())).set_index("index").drop("Unexcused", axis=1)

基本上你要添加的内容:

  • 将使用tsdf1
  • 创建的几乎为空的df复制4次
  • fillna(dict1)允许在列
  • 中填充所有NaN的静态值
  • 连接4个dfs,我们仍然需要删除一些重复项,因为csv的原始值重复了4次
  • 删除重复项,我们需要索引来保持添加的值,因此reset_index后跟`set_index(“index”)
  • 最后删除 Unexcused

最后输出一些:

In [5]: final_res["2013-01-2"]
Out[5]: 
           Description  Instances      Shift
index                                       
2013-01-02  Discipline        0.0  1st Cooks
2013-01-02    Vacation        0.0  1st Cooks
2013-01-02    Vacation        0.0  2nd Baker
2013-01-02  Discipline        0.0  2nd Baker

In [6]: final_res["2014-01-2"]
Out[6]: 
           Description  Instances       Shift
index                                        
2014-01-02  Discipline        1.0   2nd Baker
2014-01-02    Vacation        2.0   1st Cooks
2014-01-02  Discipline        3.0   2nd Baker
2014-01-02    Vacation        1.0   1st Cooks
1