簡體   English   中英

處理總付款的數據 python

[英]data proccesing to make total payment python

編號 TGL/瓦克圖 密碼 科德驗證
阿利夫 100061 17/12/2022 07:53:26 西迪克賈里
阿利夫 100061 17/12/2022 13:00:25 西迪克賈里
阿利夫 100061 19/12/2022 07:54:59 西迪克賈里
阿利夫 100061 19/12/2022 16:18:14 西迪克賈里
阿利夫 100061 20/12/2022 07:55:54 西迪克賈里
阿利夫 100061 20/12/2022 16:16:16 西迪克賈里
阿利夫 100061 21/12/2022 07:54:46 西迪克賈里
阿利夫 100061 21/12/2022 16:15:41 西迪克賈里
阿利夫 100061 22/12/2022 07:55:54 西迪克賈里
阿利夫 100061 22/12/2022 16:15:59 西迪克賈里
阿利夫 100061 23/12/2022 07:56:26 西迪克賈里
阿利夫 100061 23/12/2022 16:16:56 西迪克賈里
布迪 100063 17/12/2022 07:45:28 西迪克賈里
布迪 100063 17/12/2022 13:00:23 西迪克賈里
布迪 100063 19/12/2022 07:39:29 西迪克賈里
布迪 100063 19/12/2022 16:17:37 西迪克賈里
布迪 100063 20/12/2022 13:13:06 西迪克賈里
布迪 100063 20/12/2022 16:16:14 西迪克賈里
布迪 100063 21/12/2022 07:39:54 西迪克賈里
布迪 100063 21/12/2022 16:15:38 西迪克賈里
布迪 100063 22/12/2022 07:39:02 西迪克賈里
布迪 100063 22/12/2022 16:15:55 西迪克賈里
布迪 100063 23/12/2022 07:41:13 西迪克賈里
布迪 100063 23/12/2022 16:16:25 西迪克賈里

所以我想從那個原始的 excel 文件制作一個應用程序到 output 的 waging 系統

!pip install xlrd
import pandas as pd
from datetime import time, timedelta
import openpyxl

from google.colab import drive
drive.mount('/content/drive')

# Read the Excel file
path = '/content/drive/MyDrive/Colab Notebooks/Book1.xlsx'
df = pd.read_excel(path)


# Convert the 'Tgl/Waktu' column to datetime format
df['Tgl/Waktu'] = pd.to_datetime(df['Tgl/Waktu'])

# Extract the date and time from the 'Tgl/Waktu' column
df['Date'] = df['Tgl/Waktu'].dt.date
df['Time'] = df['Tgl/Waktu'].dt.time

# Group the data by employee name and date
grouped_df = df.groupby(['Nama', 'Date'])

# Set the overtime threshold to 16:30:00
overtime_threshold = time(hour=16, minute=30)

# Iterate over the grouped data
for (name, date), group in grouped_df:
    # Calculate the total work hours and overtime hours for each employee on each day
    start_time = group['Time'].min()
    end_time = group['Time'].max()
    total_hours = (timedelta(hours=end_time.hour, minutes=end_time.minute, seconds=end_time.second) - 
                   timedelta(hours=start_time.hour, minutes=start_time.minute, seconds=start_time.second)).total_seconds() / 3600
    if total_hours > 8:
        hours_worked = 8
        if end_time > overtime_threshold:
          overtime_hours += (end_time - overtime_threshold).total_seconds() / 3600
    else:
        hours_worked = total_hours
        overtime_hours = 0
    if end_time > overtime_threshold:
        overtime_hours += (end_time - overtime_threshold).total_seconds() / 3600
    # Calculate the payment for each employee on each day
    payment_each_date = 75000 * hours_worked + 50000 * overtime_hours
    
       # Add the total work hours, overtime hours, and payment as new columns to the dataframe
    df.loc[(df['Nama'] == name) & (df['Date'] == date), 'Hours Worked'] = hours_worked
    df.loc[(df['Nama'] == name) & (df['Date'] == date), 'Overtime Hours'] = overtime_hours
    df.loc[(df['Nama'] == name) & (df['Date'] == date), 'Payment Each Date'] = payment_each_date

# Print the resulting dataframe
print(df)

# write DataFrame to excel
df.to_excel(excel_writer=r'/content/drive/MyDrive/Colab Notebooks/test.xlsx')

從那個代碼我想添加另一列是總付款,我有一個想法在每個按名稱和不同日期分組的日期添加付款

我怎么做?

我已經嘗試使用 groupby nama 然后每天總結付款但它仍然錯誤

更新的答案

如果要在計算每個Nama值的總付款之前刪除重復的date行,則首先按Namadate分組,取最大值(最小值相同,因為兩個值相等),按Nama和對Payment Each Date值求和。 這會生成一個包含每個Nama的總付款的系列, s_total ,然后可以將其加入到原始的 dataframe 以獲得所需的Total Payment列:

s_total = df.groupby(['Nama', 'Date'])['Payment Each Date'].max().groupby('Nama').sum().rename('Total Payment')
df = df.merge(s_total, how='left', on='Nama')

左連接更安全,因為它將始終保留df的所有行以防萬一。

更新 Output df

在此處輸入圖像描述

原始答案

如果您只是想在一個新列中對所有Payment Each Date值求和,則在for循環代碼塊之后和打印 df 的行之前添加以下代碼行:

df['Total Payment'] = df.groupby('Nama')['Payment Each Date'].transform('sum')

transform負責將包含每個Nama值之和的序列的索引與原始 df 對齊。

原始 Output df : 在此處輸入圖像描述

這篇 SO 帖子有一些類似的答案: How do I create a new column from the output of pandas groupby().sum()?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM