简体   繁体   English

在Python中向CSV添加新列

[英]Adding new column to CSV in Python

I would like to add a new column to my CSV in Python. 我想在Python中为CSV添加新列。 I understand that in Python I will have to run the CSV and rewrite a new one as an output to add a column. 我了解在Python中,我将必须运行CSV并重写一个新的CSV作为输出以添加列。

I want to run though each UserID and assign a UniqueID to it. 我想通过每个用户ID运行并为其分配一个唯一ID。

This is my input : 这是我的输入

UserID,name
a,alice
a,alice
b,ben
c,calvin
c,calvin
c,calvin

This is my desired output : 这是我想要的输出

UniqueID,UserID,name
1,a,alice
1,a,alice
2,b,ben
3,c,calvin
3,c,calvin
3,c,calvin

I am new to Python and was wondering if anyone can show me how this can be done. 我刚接触Python,想知道是否有人可以向我展示如何做到这一点。 Thanks. 谢谢。

Here is my code so far: 
import csv
import operator

temp_index = 0

with open("./coordinates.csv") as all_coordinates_csv:
    coordinate_reader = csv.reader(all_coordinates_csv, delimiter=",")

    sort = sorted(coordinate_reader,key=operator.itemgetter(0))

with open("./sorteduserid.csv","wb") as sorteduser_csv:
    csv_writer = csv.writer(sorteduser_csv,delimiter=",")
    csv_writer.writerows(sort)

Take a try with my code: 试试看我的代码:

import csv
import uuid

is_first = True
with open('test.csv', newline='') as input_file:  
    with open('output.csv', 'w', newline='') as output_file:
        writer = csv.writer(output_file)
        reader = csv.reader(input_file)
        for row in reader:
            if is_first:
                row.insert(0, 'UniqueID')
                is_first = False
            else:
                row.insert(0, str(uuid.uuid4()))

            writer.writerow(row)

This solution is based on your own attempt: 此解决方案基于您自己的尝试:

import csv
import operator
from itertools import groupby

with open("./coordinates.csv") as all_coordinates_csv:
    coordinate_reader = csv.reader(all_coordinates_csv, delimiter=",")

    sort = sorted(coordinate_reader,key=operator.itemgetter(0))

grouped = []
for key, group in groupby(sort, lambda x: x[0]):
    grouped.append(list(group))

data_out = [];
data_out.append(['Unique_ID', (grouped[0])[0][0], (grouped[0])[0][1]])

user_id = 1
for group in grouped[1:]:
    for user in group:
        data_out.append([user_id,user[0],user[1]])
    user_id += 1

with open("./sorteduserid.csv","wb") as sorteduser_csv:
    csv_writer = csv.writer(sorteduser_csv,delimiter=",")
    csv_writer.writerows(data_out)

After you sort your input the program uses groupby to group the values by UserID in sort . 在对输入进行排序后,程序将使用groupby来按sort的UserID对值进行分组。 It then uses those grouped values in the loops to assign a unique ID to each UserID. 然后,它将在循环中使用这些分组的值为每个UserID分配一个唯一的ID。 Before the loops, it extends the header with User_ID entry. 在循环之前,它将使用User_ID条目扩展标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM