Add new column in a csv file and manipulate on the on records

2024/11/15 8:46:40

I have 4 csv files named PV.csv, Dwel.csv, Sess.csv, and Elap.csv. I have 15 columns and arouind 2000 rows in each file. At first I would like to add a new column named Var in each file and fill up the cells of the new column with the same file name. Therefore, new column 'Var' in PV.csv file will filled up by PV. Same is for other 3 files. After that I would like to manipulate the all the files as follows.

Finally I would like to merge / join these 4 files based on the A_ID and B_ID and write records into a new csv file name finalFile.csv. Any suggestion and help is appreciated.

<p>PV.csv is as follows:</p>
   
A_ID      B_ID       LO       UP     LO      UP
103       321        0        402    
103       503        192      225    433     608   
106       264        104      258    334     408
107       197        6        32     113     258    

Dwell.csv is as follows:

   
A_ID      B_ID       LO       UP     LO      UP  
103       321        40       250    517     780
103       503        80       125    435     585     
106       264        192      525    682  
107       197        324      492    542     614    

Session.csv is as follows:

   
A_ID      B_ID       LO       UP     LO      UP 
103       321        75       350    370     850     
106       264        92       225    482     608  
107       197        24       92     142    

Elapsed.csv is as follows:

   
A_ID      B_ID       LO       UP     LO      UP 
103       321        5        35     75
103       503        100      225    333     408      
106       264        102      325    582  
107       197        24       92     142     214    

First output file of PV.csv will be as follows:

Same way all rest of three files will be filled up with new column with ehrer file name, Dwell, Session, and Elapsed:

   
A_ID    B_ID      Var   LO        UP     LO      UP
103     321       PV    0         402    
103     503       PV    192       225    433     608   
106     264       PV    104       258    334     408
107     197       PV    6         32     113     258 

Final output file will be as follows:

finalFile.csv.

   
A_ID    B_ID      Var    LO        UP
103     321       PV     0         402
103     321       Dwel   40        250
103     321       Dwel   251       517
103     321       Dwel   518       780
103     321       Sess   75        350
103     321       Sess   351       370
103     321       Sess   371       850
103     321       Elap   5         35
103     321       Elap   36        75
103     503       PV     192       225
103     503       PV     226       433
103     503       PV     434       608
103     503       Dwel   80        125
103     503       Dwel   126       435
103     503       Dwel   436       585
103     503       Elap   100       225
103     503       Elap   226       333
103     503       Elap   334       408
106     264       PV     104       258
106     264       PV     259       334
106     264       PV     335       408
106     264       Dwel   192       525
106     264       Dwel   526       682
106     264       Sess   92        225
106     264       Sess   226       482
106     264       Sess   483       608
106     264       Elap   102       325
106     264       Elap   326       582
107     197       PV     6         32
107     192       PV     33        113
107     192       PV     114       258
107     192       Dwel   324       492
107     192       Dwel   493       542
107     192       Dwel   543       614
107     192       Sess   24        92
107     192       Sess   93        142
107     192       Elap   24        92
107     192       Elap   93        142
107     192       Elap   143       214
Answer

You should use python builtin csv module.

To create the final csv file you can do like this. Read through each file, add the new column value to every row and write it to the new file

import csvwith open('finalcsv.csv', 'w') as outcsv:writer = csv.writer(outcsv)writer.writerow(['a','b','c','etc','Var']) # write final headersfor filename in ['PV.csv','Dwel.csv','Sess.csv','Elap.csv']:with open(filename) as incsv:val = filename.split('.csv')[0]reader = csv.reader(incsv) # create reader objectreader.next() # skip the headersfor row in reader:writer.writerow(row+[val])
https://en.xdnf.cn/q/119069.html

Related Q&A

Xpath returns null

I need to scrape the price of this page: https://www.asos.com/monki/monki-lisa-cropped-vest-top-with-ruched-side-in-black/prd/23590636?colourwayid=60495910&cid=2623 However it is always returning …

I am getting an Index error as list out of range. I have to scan through many lines

import nltk import random from nltk.tokenize import sent_tokenize, word_tokenizefile = open("sms.txt", "r") for line in file:#print linea=word_tokenize(line)if a[5] == SBI and a[6]=…

How can i append two classes in JQuery

I have implemented a chat box message (live chat) using django and now i want to add css, but i have problem on how to append multiple classes on messege sent. For example, i want to show other user me…

After installing PyBluez on Windows8.1 I get DLL %1 not valid win32 app

I have installed PyBluez-0.22.win32.exe on a 64bit machine with Python 2.7 (they didnt have a 64bit version). Then I get the following error: ImportError:DLL load failed:%1 is not valid Win32 applicati…

Flask Apache on AWS EC2 - Read/Write Failing

So Ive been stumped by this problem for a day now. Im relatively new to AWS EC2 so have been experimenting with Python Flask apps on it.I have an Ubuntu instance, and can get a flask app to run fine on…

AttributeError: unicode object has no attribute pop

I have this piece of python code in gae.def post(self):cases=self.request.get(cases)while cases:logging.info("cases: %s " % cases)case=cases.pop()Which produces this log.INFO 2012-09-19 2…

How to set platform when using pip download command

I want to download some pacakges(tensorflow, keras, imbalanced, xgboost, lightgbm, catboost) for centos 7.4 and python 3.7 on mac.How should i set platform name and ant other settings?I used below com…

How to split qr codes in separate images?

I have hundreds of pictures with qr codes (sometimes there are 0, 1, 2 or more qr codes on one page...but they are always in one line). I want to decode the qr codes from left to right. My idea is to s…

Python methods from csv

I am working on an assignment where I create "instances" of cities using rows in a .csv, then use these instances in methods to calculate distance and population change. Creating the instance…

Convert string numpy.ndarray to float numpy.ndarray

I have one problem. How can I convert:import numpy as npa = np.array([[0.1 0.2 0.3], [0.3 0.4 0.5], [0.5 0.6 0.7]])To:b = np.array([[0.1,0.2,0.3], [0.3,0.4,0.5], [0.5,0.6,0.7]])