簡體   English   中英

將行中的值追加到新列中?

[英]Appending values in rows into new columns?

我在CSV中有此數據:

first, middle, last, id, fte
Alexander,Frank,Johnson,460700,1 
Ashley,Jane,Smith,470000,.5 
Ashley,Jane,Smith,470000,.25 
Ashley,Jane,Smith,470000,.25 
Steve,Robert,Brown,460001,1

我需要找到具有相同ID號的人員行,然后將這些行的FTE合並到同一行中。 我還需要為沒有重復的行添加0。 例如(使用上面的數據):

first, middle, last, id, fte1, fte2, fte3, fte4
Alexander,Frank,Johnson,460700,1,0,0,0
Ashley,Jane,Smith,470000,.5,.25,.25,0
Steve,Robert,Brown,460001,1,0,0,0

基本上,我們正在尋找人們擁有的工作。 有些人每周工作40個小時(1.0 FTE),有些人每周工作20個小時兩次(0.5和0.5 FTE),有些人可能每周工作10個小時4個(.25,.25, .25和.25 FTE),有些可能具有其他組合。 每個員工只獲得一行數據,因此我們需要在同一行上安裝FTE。

到目前為止,這就是我們所擁有的。 目前,我們當前的代碼僅在具有兩個FTE時才有效。 如果它們有3個或4個,它只會用后兩個覆蓋它們(因此,如果有3個,它將給我們2和3。如果有3個,則給我們3和4個)。

f = open('data.csv')
csv_f = csv.reader(f)
dataset = []
for row in csv_f:
    dictionary = {}
    dictionary["first"] = row[2]
    dictionary["middle"] = row[3]
    dictionary["last"] = row[4]
    dictionary["id"] = row[10]
    dictionary["fte"] = row[12]
    dataset.append(dictionary)

def is_match(dict1, dict2):
    return (dict1["id"] == dict2["id"])

def find_match(dictionary, dict_list):
    for index in range(0, len(dict_list)):
        if is_match(dictionary, dict_list[index]):
            return index
    return -1

def process_data(dataset):
    result = []
    for index in range(1, len(dataset)):
        data_dict = dataset[index]
        match_index = find_match(data_dict, result)
        id = str(data_dict["id"])
        if match_index == -1:
            result.append(data_dict)
        else:
            (result[match_index])["fte2"] = data_dict["fte"]
    return result

f.close()

for row in process_data(dataset):
    print(row)

任何幫助將不勝感激! 謝謝!

我會說要使用pandas庫來簡化它。 您可以將group by與聚合一起使用。 以下是在此處https://www.tutorialspoint.com/python_pandas/python_pandas_groupby.htm提供的聚合示例之后的示例。

import pandas as pd
import numpy as np

df = pd.read_csv('filename.csv')

grouped = df.groupby('id')
print grouped['fte'].agg(np.sum)

暫無
暫無

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

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