如何通过移动一行使用python在CSV文件中创建新列 [英] How do create new column in csv file using python by shifting one row

查看:165
本文介绍了如何通过移动一行使用python在CSV文件中创建新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下所示的CSV文件.它是具有成千上万条记录的巨大文件.

I have CSV file like below. It is huge file with thousands of records.

input.csv

input.csv

No;Val;Rec;CSR
0;10;1;1200
0;100;2;1300
0;100;3;1300
0;100;4;1400
0;10;5;1200
0;11;6;1200

我想通过在第一列否"之后添加新列"PSR"来创建output.csv文件.此列值取决于列"PSR"的值.对于第一行,"PSR"应为零.从下一个记录开始,它取决于上一行中的"CSR"值.如果当前记录和先前记录的CSR值相同,则"PSR"应为零.如果不是,则PSR值应具有先前的CSR值.例如,第二行的CSR值为1300,与第一条记录的CSR值为1200.因此,第二行的PSR值应为1200.在第二行和第三行中,CSR值相同.因此,第三行的PSR值应为零.因此,新值PSR取决于当前和先前字段中的CSR值.

I want to create output.csv file by adding new column "PSR" after 1st column "No". This column value depends on column "PSR" Value. For 1st row, "PSR" shall be zero. From next record on-wards, it depends on "CSR" value in previous row. If present and previous record CSR value is same, then "PSR" shall be zero. If not, PSR value shall have the previous CSR value. For exmple, Value of CSR in 2nd row is 1300 which is different to the value in 1st record ( it is 1200). So PSR value for 2nd row shall be 1200. Where in 2nd and 3rd row, CSR value is same. So PSR value for 3rd row shall be zero. So new value PSR depends on CSR value in present and previous field.

Output.csv

Output.csv

No;PCR;Val;Rec;CSR
0;0;10;1;1200
0;1200;100;2;1300
0;0;100;3;1300
0;1300;100;4;1400
0;1400;10;5;1200
0;0;11;6;1200

我的方法:

  1. 使用csv.reader并遍历列表中的对象.将第5列复制到列表中的第2列.向下移动一排.
  2. 然后检查第二列和第五列中的值(PCR和CSR),如果两个值相同.将PCR值替换为零.

我在第一步编码时遇到了问题.我能够复制该列,但不能移动它.第二步也很简单.

I have problem in getting 1st step coded. I am able to duplicate the column but not able to shift it. Also 2nd step is quite straightforward.

此外,我不确定这种方法是否正确.任何指针/建议都将真正有用.

Also, I am not sure whether this approach is correct Any pointers/recommendation would be really helpful.

注意:我无法在CentOS上安装Pandas.因此,没有此模块的帮助会更好.

Note: I am not able to install Pandas on CentOS. So help without this module would be better.

我的代码:

with open('input.csv', 'r') as input, open('output.csv', 'w') as output:
        reader = csv.reader(input, delimiter = ';')
        writer = csv.writer(output, delimiter = ';')
        mylist = []                                        
        header = next(reader)                           
        mylist.append(header)
        for rec in reader:
                mylist.append(rec)                      
                rec.insert(1, rec[3])
                mylist.append(rec)
        writer.writerows(mylist)

推荐答案

如果您开放使用非python解决方案,那么awk可能是一个不错的选择:

If your open to non-python solutions then awk could be a good option:

awk 'NR==1{$2="PSR;"$2}NR>1{$2=($4==a?0";"$2:+a";"$2);a=$4}1' FS=';' OFS=';' file
No;PSR;Val;Rec;CSR
0;0;10;1;1200
0;1200;100;2;1300
0;0;100;3;1300
0;1300;100;4;1400
0;1400;10;5;1200
0;0;11;6;1200

Awk随几乎所有Linux发行版一起发行,并且正是针对此类任务而设计的.它将遍历您的文件.在末尾> output.csv处添加重定向,以将输出保存到文件中.

Awk is distributed with pretty much all Linux distributions and was designed exactly for this kind of task. It will blaze through your file. Add a redirection to the end > output.csv to save the output in a file.

使用相同逻辑的简单python方法:

A simple python approach using the same logic:

#!/usr/bin/env python

last = "0"

with open('input.csv') as csv:
    print next(csv).strip().replace(';', ';PSR;', 1)
    for line in csv:
        field = line.strip().split(';')
        if field[3] == last: field.insert(1, "0")
        else: field.insert(1, last)
        last = field[4]
        print ';'.join(field)

产生相同的输出:

$ python parse.py
No;PSR;Val;Rec;CSR
0;0;10;1;1200
0;1200;100;2;1300
0;0;100;3;1300
0;1300;100;4;1400
0;1400;10;5;1200
0;0;11;6;1200

同样,只需重定向输出以保存它:

Again just redirect the output to save it:

$ python parse.py > output.csv 

这篇关于如何通过移动一行使用python在CSV文件中创建新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆