2016-01-16 26 views
1

我有兩個員工數據如下的數據框。一個數據文件包含員工數據,其中包括員工生病的日期,另一個數據文件具有員工工作日期(即以日期範圍表示)。 我想通過查看某個員工的「病假」落在「工作範圍」中的哪個位置來合併這兩個文件(希望在熊貓中)。例如,在下面的圖像/數據中,員工1在2015年11月25日,2015年12月23日和2015年12月10日生病。這些工作範圍分別在2015年11月21日 - 2015年11月29日,2015年12月21日 - 2015年12月29日和2015年10月9日 - 2015年10月17日。如何通過在一個數據幀中查看另一個數據幀中日期範圍內的日期來組合Pandas數據框?

員工工作日期的數據:

╔══════════╦════════════╦════════════╗ 
 
║ Employee ║ datein ║ dateout ║ 
 
╠══════════╬════════════╬════════════╣ 
 
║  1 ║ 11/21/2015 ║ 11/29/2015 ║ 
 
║  2 ║ 12/9/2015 ║ 12/14/2015 ║ 
 
║  3 ║ 11/10/2015 ║ 11/19/2015 ║ 
 
║  4 ║ 11/11/2015 ║ 11/17/2015 ║ 
 
║  5 ║ 11/30/2015 ║ 12/8/2015 ║ 
 
║  1 ║ 12/21/2015 ║ 12/29/2015 ║ 
 
║  2 ║ 1/7/2016 ║ 1/12/2016 ║ 
 
║  3 ║ 12/10/2015 ║ 12/19/2015 ║ 
 
║  4 ║ 12/10/2015 ║ 12/16/2015 ║ 
 
║  5 ║ 12/30/2015 ║ 1/7/2016 ║ 
 
║  1 ║ 10/9/2015 ║ 10/17/2015 ║ 
 
║  2 ║ 10/27/2015 ║ 11/1/2015 ║ 
 
║  3 ║ 9/28/2015 ║ 10/7/2015 ║ 
 
║  4 ║ 9/29/2015 ║ 10/5/2015 ║ 
 
╚══════════╩════════════╩════════════╝

員工SICK日期的數據:

╔══════════╦════════════╦═══════════╗ 
 
║ Employee ║ sickDate ║ sickness ║ 
 
╠══════════╬════════════╬═══════════╣ 
 
║  1 ║ 11/25/2015 ║ flu  ║ 
 
║  10 ║ 11/21/2015 ║ hd  ║ 
 
║  21 ║ 9/20/2015 ║ other  ║ 
 
║  1 ║ 12/23/2015 ║ other  ║ 
 
║  4 ║ 12/13/2015 ║ vacationx ║ 
 
║  7 ║ 7/21/2015 ║ cough  ║ 
 
║  3 ║ 10/1/2015 ║ rash  ║ 
 
║  4 ║ 10/5/2015 ║ other  ║ 
 
║  5 ║ 1/7/2016 ║ eyex  ║ 
 
║  2 ║ 12/12/2015 ║ tanx  ║ 
 
║  1 ║ 10/12/2015 ║ fatiguex ║ 
 
╚══════════╩════════════╩═══════════╝

統一數據:

╔══════════╦════════════╦════════════╦════════════╦═══════════╗ 
 
║ Employee ║ datein ║ dateout ║ sickDate ║ sickness ║ 
 
╠══════════╬════════════╬════════════╬════════════╬═══════════╣ 
 
║  1 ║ 11/21/2015 ║ 11/29/2015 ║ 11/25/2015 ║ flu  ║ 
 
║  2 ║ 12/9/2015 ║ 12/14/2015 ║ 12/12/2015 ║ tanx  ║ 
 
║  3 ║ 11/10/2015 ║ 11/19/2015 ║   ║   ║ 
 
║  4 ║ 11/11/2015 ║ 11/17/2015 ║   ║   ║ 
 
║  5 ║ 11/30/2015 ║ 12/8/2015 ║   ║   ║ 
 
║  1 ║ 12/21/2015 ║ 12/29/2015 ║ 12/23/2015 ║ other  ║ 
 
║  2 ║ 1/7/2016 ║ 1/12/2016 ║   ║   ║ 
 
║  3 ║ 12/10/2015 ║ 12/19/2015 ║   ║   ║ 
 
║  4 ║ 12/10/2015 ║ 12/16/2015 ║ 12/13/2015 ║ vacationx ║ 
 
║  5 ║ 12/30/2015 ║ 1/7/2016 ║ 1/7/2016 ║ eyex  ║ 
 
║  1 ║ 10/9/2015 ║ 10/17/2015 ║ 10/12/2015 ║ fatiguex ║ 
 
║  2 ║ 10/27/2015 ║ 11/1/2015 ║   ║   ║ 
 
║  3 ║ 9/28/2015 ║ 10/7/2015 ║ 10/1/2015 ║ rash  ║ 
 
║  4 ║ 9/29/2015 ║ 10/5/2015 ║ 10/5/2015 ║ other  ║ 
 
╚══════════╩════════════╩════════════╩════════════╩═══════════╝


我怎麼做,在大熊貓或Python? (謝謝你的幫助!)

回答

0

你需要這個數據放到pd.DataFrame(...)DF1set_index('Employee')

╔══════════╦════════════╦════════════╗ 
 
║ Employee ║ datein ║ dateout ║ 
 
╠══════════╬════════════╬════════════╣ 
 
║  1 ║ 11/21/2015 ║ 11/29/2015 ║ 
 
║  2 ║ 12/9/2015 ║ 12/14/2015 ║ 
 
║  3 ║ 11/10/2015 ║ 11/19/2015 ║ 
 
║  4 ║ 11/11/2015 ║ 11/17/2015 ║ 
 
║  5 ║ 11/30/2015 ║ 12/8/2015 ║ 
 
║  1 ║ 12/21/2015 ║ 12/29/2015 ║ 
 
║  2 ║ 1/7/2016 ║ 1/12/2016 ║ 
 
║  3 ║ 12/10/2015 ║ 12/19/2015 ║ 
 
║  4 ║ 12/10/2015 ║ 12/16/2015 ║ 
 
║  5 ║ 12/30/2015 ║ 1/7/2016 ║ 
 
║  1 ║ 10/9/2015 ║ 10/17/2015 ║ 
 
║  2 ║ 10/27/2015 ║ 11/1/2015 ║ 
 
║  3 ║ 9/28/2015 ║ 10/7/2015 ║ 
 
║  4 ║ 9/29/2015 ║ 10/5/2015 ║ 
 
╚══════════╩════════════╩════════════╝

那麼這個數據放到pd.DataFrame(...)DF2set_index('Employee')

╔══════════╦════════════╦═══════════╗ 
 
║ Employee ║ sickDate ║ sickness ║ 
 
╠══════════╬════════════╬═══════════╣ 
 
║  1 ║ 11/25/2015 ║ flu  ║ 
 
║  10 ║ 11/21/2015 ║ hd  ║ 
 
║  21 ║ 9/20/2015 ║ other  ║ 
 
║  1 ║ 12/23/2015 ║ other  ║ 
 
║  4 ║ 12/13/2015 ║ vacationx ║ 
 
║  7 ║ 7/21/2015 ║ cough  ║ 
 
║  3 ║ 10/1/2015 ║ rash  ║ 
 
║  4 ║ 10/5/2015 ║ other  ║ 
 
║  5 ║ 1/7/2016 ║ eyex  ║ 
 
║  2 ║ 12/12/2015 ║ tanx  ║ 
 
║  1 ║ 10/12/2015 ║ fatiguex ║ 
 
╚══════════╩════════════╩═══════════╝

最後,df = df1.join(df2).reset_index()

+0

對不起時,我把它應用到我的「做大」的數據文件,這是行不通的。我已將這些數據放在Google文檔中。 2個選項卡。 https://docs.google.com/spreadsheets/d/1Gyw2if41MU8wSbjto6rQfLW9x68_kqTOzsqcZ-laSqA/edit?usp=sharing – Codetradr

0

考慮一個內部和外部pandas merge方法。下面假定日期是在datetime格式可從字符串對象需要轉換:

workdf['datein'] = pd.to_datetime(workdf['datein']) 
workdf['dateout'] = pd.to_datetime(workdf['dateout']) 
sickdf['sickDate'] = pd.to_datetime(sickdf['sickDate']) 

# INNER MERGE ON BOTH DFs WHERE SICK DAYS REPEAT FOR MATCHING EMPLOYEE ROW IN WORK DAYS 
mergedf = pd.merge(workdf, sickdf, on='Employee', how="inner") 

# OUTER MERGE TO KEEP ALL WORK DAY RECORDS WITH FILTERED SICK DAYS DATA SET 
finaldf = pd.merge(mergedf[(mergedf['sickDate'] - mergedf['datein'] >= 0) & 
          (mergedf['dateout'] - mergedf['sickDate'] >= 0)], 
        workdf, on=['Employee', 'datein', 'dateout'], how="outer") 

finaldf = finaldf.sort(['Employee','datein','dateout']).reset_index(drop=True) 

結果

# Employee  datein  dateout  sickDate sickness 
#0   1 2015-10-09 2015-10-17 2015-10-12 fatiguex 
#1   1 2015-11-21 2015-11-29 2015-11-25  flu 
#2   1 2015-12-21 2015-12-29 2015-12-23  other 
#3   2 2015-10-27 2015-11-01   NaT  NaN 
#4   2 2015-12-09 2015-12-14 2015-12-12  tanx 
#5   2 2016-01-07 2016-01-12   NaT  NaN 
#6   3 2015-09-28 2015-10-07 2015-10-01  rash 
#7   3 2015-11-10 2015-11-19   NaT  NaN 
#8   3 2015-12-10 2015-12-19   NaT  NaN 
#9   4 2015-09-29 2015-10-05 2015-10-05  other 
#10   4 2015-11-11 2015-11-17   NaT  NaN 
#11   4 2015-12-10 2015-12-16 2015-12-13 vacationx 
#12   5 2015-11-30 2015-12-08   NaT  NaN 
#13   5 2015-12-30 2016-01-07 2016-01-07  eyex 
+0

對不起,當我將它應用到我的「更大」數據文件時,這不起作用。我已將這些數據放在Google文檔中。 2個選項卡。 https://docs.google.com/spreadsheets/d/1Gyw2if41MU8wSbjto6rQfLW9x68_kqTOzsqcZ-laSqA/edit?usp=sharing – Codetradr

+0

您收到什麼錯誤?什麼不工作?一定要調整實際的字段名稱。請考慮刪除Year,Month,Day,MonthNo列,因爲熊貓在datetime字段中保留該列。 – Parfait

相關問題