我为我的 DataFrame 对象创建了一个字典。我创建了一个函数来根据 DataFrame 中每周工作的小时数计算工作天数和加班时间。然后,我根据我定义的变量计算每周工作时间,并根据这些答案创建一个新的数据框架。然后我将两个 DataFrame 合并在一起,但结果是错误的,因为相应地合并数据,它通过添加到列表中来合并数据。我该如何解决这个问题?我需要结果 DataFrame 与彼此相邻的数据 DataFrame 相对应。例如,员工 ID 1 每周在远程站点工作 29 小时,工作天数和加班天数不对应。我该如何解决这一切?
import pandas as pd
data = {
'Employee_ID': [1, 2, 3, 4, 5],
'Employment_Type': ['Remote', 'In-Office', 'Remote', 'Remote', 'Remote'],
'Hours_Worked_Per_Week': [29.0, 45.0, 34.0, 25.0, 50.0]
}
df = pd.DataFrame(data)
def calculate_days_and_overtime(df):
results = []
for index, row in df.iterrows():
for index, row in df.iterrows():
total_hours = row['Hours_Worked_Per_Week']
regular_hours_per_day = 8
days_per_week = 5
total_regular_hours = days_per_week * regular_hours_per_day
overtime = max(0, total_hours - total_regular_hours)
regular_days = min(days_per_week, total_hours // regular_hours_per_day)
total_hours -= regular_days * regular_hours_per_day
overtime_days = overtime // regular_hours_per_day
remaining_overtime_hours = overtime % regular_hours_per_day
total_days_worked = regular_days + overtime_days + (1 if remaining_overtime_hours else 0)
results.append({
'Employee_ID' : row['Employee_ID'],
'Regular Days' : regular_days,
'Overtime Days' : overtime_days,
'Remaining Overtime Hours' : remaining_overtime_hours,
'Total Days Worked' : total_days_worked,
'Work Location' : row['Employment_Type']
})
return pd.DataFrame(results)
results_df = calculate_days_and_overtime(df)
merged_df = pd.concat([df, results_df], ignore_index=True)
merged_df
import pandas as pd
data = {
'Employee_ID': [1, 2, 3, 4, 5],
'Employment_Type': ['Remote', 'In-Office', 'Remote', 'Remote', 'Remote'],
'Hours_Worked_Per_Week': [29.0, 45.0, 34.0, 25.0, 50.0]
}
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
df = pd.DataFrame(data)
def calculate_days_and_overtime(df):
results = []
for index, row in df.iterrows():
total_hours = row['Hours_Worked_Per_Week']
regular_hours_per_day = 8
days_per_week = 5
total_regular_hours = days_per_week * regular_hours_per_day
overtime = max(0, total_hours - total_regular_hours)
regular_days = min(days_per_week, total_hours // regular_hours_per_day)
total_hours -= regular_days * regular_hours_per_day
overtime_days = overtime // regular_hours_per_day
remaining_overtime_hours = overtime % regular_hours_per_day
total_days_worked = regular_days + overtime_days + (1 if remaining_overtime_hours else 0)
results.append({
'Employee_ID': row['Employee_ID'],
'Regular Days': regular_days,
'Overtime Days': overtime_days,
'Remaining Overtime Hours': remaining_overtime_hours,
'Total Days Worked': total_days_worked,
'Work Location': row['Employment_Type']
})
return pd.DataFrame(results)
print("=== df ===")
print(df)
results_df = calculate_days_and_overtime(df)
print("=== results_df ===")
print(results_df)
merged_df = pd.merge(df, results_df, on='Employee_ID')
print("=== merged_df ===")
print(merged_df)