Problems regarding csv.DictReader - python

list3 = []
with open('**directory**') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
list3.append(row)
I'm completely new to data analysis using Python, and require some assistance.
The file I'm accessing contains data from 5 people (CSV file). There are 3 columns - participant number, pre-task Score, and post-task Score.
I'm essentially trying to access this file (using csv.DictReader) and manipulate the data. By this, I mean I want to calculate the difference between the post-task score and pre-task score, for each participant, and print this to the screen.
However, I'm not sure how to do this. I can print each row to the screen, and I can save each row in a list (as I've done above) - but I'm clueless as to how I am to manipulate/deal with this data. I'm wondering if there is something better than the module I'm currently using.

Calculating the difference between the second and third columns in a CSV file can be accomplished as follows:
import csv
with open('file.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
# skip the header row, remove this next line if there is no header
next(reader, None)
for row in reader:
difference = float(row[2]) - float(row[1])
print str(difference)

Related

Reading columns of data for every point in a csv file in Python

I want to read the second column of data with the title nodes and assign to a variable with the same name for each point of t1.
import csv
with open('Data_10x10.csv', 'r') as f:
csv_reader = csv.reader(f)
The data looks like
csv_reader = csv.reader(f) is a Generator. So you can skip the headers by execute heading = next(csv_reader).
I would just use a dictionary data_t1 for storing node data with key name of column t1.
Try below one.
import csv
with open('Data_10x10.csv', 'r') as f:
data_t1={}
csv_reader = csv.reader(f)
# Skips the heading
heading = next(csv_reader)
for row in csv_reader:
data_t1[row[0]] = row[1]
Accessing data (key should be value of you t1 column, in this case '0', '1' etc.)
print(data_t1['0'])
print(data_t1['1'])
If you want to create dynamic variables with the same name for each point of t1 It is really bad idea. If your csv has lot of rows maybe millions, it will create millions of variables. So use dictionary with key and values.

csv file parsing and making it dict

i have a .csv file trying to make it in a dict. I tried pandas and csv.DictReader mostly but until now i can print the data (not in the way i want) with the DictReader.
So the main problem is that the file is like
header;data (1 column)
for about 50 rows and after that it changes the schema like
header1;header2;header3;header4
in row 50 and row 50+
data1;data2;data3;data4 etc..
with open(filename, 'r', encoding='utf-16') as f:
for line in csv.DictReader(f):
print(line)
thats the code i have for now.
Thanks for your help.
You can't use DictReader for this, because it requires all the rows to have the same fields.
Use csv.reader and check the length of the row that it returns. When the length changes, treat that as a new header.
Hopefully you don't have adjacent sections of the file that have the same number of fields but different headers. It will be difficult for the script to detect when the section changes.
data = []
with open(filename, 'r', encoding='utf-16') as f:
r = csv.reader(f, delimiter=';')
# process first 52 rows in format header;data
for _ in range(52):
row = next(r)
data.append({row[0]: row[1]})
# rest of file is a header row followed by variable number of data rows
header = next(r)
for row in r:
if len(row) != len(header): # new header
header = row
continue
d = dict(zip(header, row))
data.append(d)

python add columns to csv(at the side of csv)?

i have a large csv file and can not load in memory at a time,i also want to add some columns at the side of csv,so i want to add one column once a time because that does not cost many memory,i use python and pandas,so what can i do for that.
here's my code.
def toCsv(filepath,lists):
i = 0
with open(filepath,'r+') as f:
reader = csv.reader(f)
writer = csv.writer(f)
for row in reader:
print lists
row.append(lists[i])
writer.writerows(row)
i = i+1

Reading a CSV file using Python 3

I am learning how to read CSV files using Python 3, and have been playing around with my code and have managed to read either the whole document or certain columns, however I am trying to now read only certain records that contain a certain value.
For example I want to read all records where the car is blue, how would I make it read only those records? I can't figure this out and would be grateful for any help or guidance!
import csv
with open('cars.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row['ID'], row['Make'], row['Colour'])
A simple "if" statement should suffice. See control flow docs.
import csv
with open('Cars.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['Colour'] == 'blue':
print(row['ID'] ,row ['Make'],row ['Colour'])
You can check the values while reading the rows.
with open('Cars.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
// check your values here - if car = blue
// do something with blue cars.
print(row['ID'] ,row ['Make'],row ['Colour'])
You read each row one by one and use an explicit check to filter those that you want to deal with. Then add them to an array for example, or process it in place.

How to output calculations in python?

I have a csv file created with 6 rows, 1 column (header and 5 numbers). I want to be able to do a conversion, say from centimeters to inches, and save it in a new csv with a new header.
So far I have only been able to import the csv and read it, and print it (using print row), but how can I do the conversion? Since the numbers are saved in the csv, would I have to convert the numbers to float and then write them to a new csv? I only have 5 numbers as I want to be able to just figure out the correct code, but I will use this for a lot of numbers.
I wasn't sure where the computation would be placed either. Help please! Also, this isn't homework or the like. Im just doing this for fun.
This is the code I currently have:
import csv
with open('test.csv', 'rb') as f:
reader = csv.reader(f)
next(reader, None) # I did this to skip the header I labelled Centimeters
with open('test1.csv', 'wb') as o:
writer = csv.writer(o)
for row in reader
f.close()
o.close()
I guess I dont know how to convert the number in the rows to float and then output the values. I want to just be able to multiply the number in the row by 0.393701 so that in the new csv the header is labelled inches with the output beneath in the rows.
This should work, assuming a single column (for multiple columns the handling would differ some to output all the values, but the general concept would be the same):
import csv
with open('test.csv', 'rb') as f, open('test1.csv', 'wb') as o:
reader = csv.reader(f)
writer = csv.writer(o)
# skip the header
next(reader, None)
# print the new header
writer.writerow(['inches'])
for row in reader:
newVal = float(row[0]) * 0.393701
writer.writerow([newVal])
import csv
float_rows=[]
with open('test.csv', 'rb') as f:
reader = csv.reader(f)
next(reader, None) # I did this to skip the header I labelled Centimeters
for row in reader:
comp = [ x * 0.393701 for x in map(float,row)] # do calculations and map elements to float
float_rows.append(comp)
with open('test1.csv', 'wb') as o:
writer = csv.writer(o)
writer.writerows(float_rows) # write all computed data to new csv
No close needed, with closes the files automatically.
Using map(float,iterable) is the same as [float(x) for x in my_iterable]

Categories