Question

I need help modifying this python code: a) I need to take 2 attributes of the...

I need help modifying this python code:

a) I need to take 2 attributes of the data as int - (columns (3 & 5) of my data)

b) I need to take 2 more attributes as floats -(columns 4&6 of my data)

c) I need to take 1 attribute as string (column 16 of my data)

How would I modify this code

#
# Initial version - "standard programming"
#
# Define a list for the data. The data structure will be a list of lists.
data = []
# open the file
fname = "..\data\Online_shoppers.csv"
f = open(fname, "r")
# ignore the first 1 line(header)
for i in range(2):
line = f.readline()
# loop until we run out of lines
while (line):
# strip the newline and tokenize (split on commas, in this case)
tokens = line.rstrip().split(',')
# append this record to the dataset
data.append(tokens)
# read the next line
line = f.readline()
# close the file
f.close()
# show the data
show(data)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#I am assuming you need the whole column, but the specified columns in the data type provided.If this is not the case, please clarify in comments

#
# Initial version - "standard programming"
#
# Define a list for the data. The data structure will be a list of lists.
data = []
# open the file
fname = "..\data\Online_shoppers.csv"
f = open(fname, "r")
# ignore the first 1 line(header)
for i in range(2):
line = f.readline()
# loop until we run out of lines
while (line):
# strip the newline and tokenize (split on commas, in this case)
tokens = line.rstrip().split(',')
tokens[2]=int(tokens[2])#converting to int
tokens[4]=int(tokens[4])#converting to int
tokens[3]=float(tokens[3])#converting to float
tokens[5]=float(tokens[5])#converting to float
  
# append this record to the dataset
data.append(tokens)
# read the next line
line = f.readline()
# close the file
f.close()
# show the data
show(data)

Add a comment
Know the answer?
Add Answer to:
I need help modifying this python code: a) I need to take 2 attributes of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using python 3.6 How do I incorporate 0 or negative input to raise an error and...

    Using python 3.6 How do I incorporate 0 or negative input to raise an error and let the user try again? like <=0 #loop to check valid input option. while True: try: choice = int(input('Enter choice: ')) if choice>len(header): print("Invalid choice!! Please try again and select value either:",end=" ") for i in range(1,len(header)+1): print(i,end=",") else: break choice = int(input('\nEnter choice: ')) except ValueError as ve:print('Invalid response, You need to select the number choice from the option menu') # Catch your...

  • the following python code edits BMP image file to negative but I need help with modifying...

    the following python code edits BMP image file to negative but I need help with modifying this code so it turns it the same BMP image file into a black and white instead of a negative. heres python code I have so far [pasted below] ## # This program processes a digital image by creating a negative of a BMP image. # from io import SEEK_CUR from sys import exit def main() : filename = input("Please enter the file name:...

  • I need help in Python. This is a two step problem So I have the code...

    I need help in Python. This is a two step problem So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:) 7. Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file...

  • In java netbean8.1 or 8.2 please. The class name is Stock. It has 5 private attributes...

    In java netbean8.1 or 8.2 please. The class name is Stock. It has 5 private attributes (name, symbol, numberOfShares, currentPrice and boughtPrice)and one static private attribute (numberOfStocks). All attributes must be initialized (name and symbol to “No name/ symbol yet” and numberOfShares, currentPrice and boughtPrice to 0. Create two constructors, one with no arguments and the other with all the attributes. (However, remember to increase the numberOfStocks in both constructors. Write the necessary mutators and accessors (getters and setters) for...

  • In python, PART A: I am trying to get a dictionary with size(4, 5, 6) as...

    In python, PART A: I am trying to get a dictionary with size(4, 5, 6) as keys and an array for key containing a list of values (words from file of respective size) associated with those keys I am reading from a file of strings, where I am only interested with words of length 4, 5, and 6 to compute my program reading the text file line by line: At first, I have an empty dictionary then to that I...

  • I need to create the "nnclassifier using the euclidean distance formula to find nearest neighbor. I...

    I need to create the "nnclassifier using the euclidean distance formula to find nearest neighbor. I am using the inis dataset from seabom. I have loaded the data and split the database into values and species. I have created a function to find the nearest neighbor by also calculating the euclidean distance and appending them to a distance list. I am able to see the positions of the nearest neighbors. Smart numpy as Import pandas as pd Import seaborn as...

  • Python function: This is my code and I don't know how to add the column to...

    Python function: This is my code and I don't know how to add the column to the csv file: def write_with_averages(read,write): with open (read,'r') as f_in: header = f_in.readline() reader=csv.reader(f_in) with open (write,'w') as f_out: writer=csv.writer(f_out) New_data=[] for Name, Test1, Test2, Test3 in reader: Total=(float(Test1)+float(Test2)+float(Test3)) average=Total/3 New_data.append(average) Hint: Skip the first line of the file as it is a header line Hint: All values come in as strings, so convert to floats before mathematical comparisons and calculations. File Format Name,Test1,Test2,Test3...

  • Consider python please for this problem: I did a python program to read the first 20 lines from a...

    Consider python please for this problem: I did a python program to read the first 20 lines from a file and here is the code holder = 20 lines = 3 file = "123456.pssm" _file = ".txt" while 1:         out_file = input("[OUTPUT] - Please enter the name of file: ") + _file         if (not _file):         print("Filename not specified - Please Try Again\n")         continue     break with open(_file, "a") as e:     with open(file, "r") as f:         for num, line in enumerate(f, 1):                        ...

  • Use of search structures! how can i make this program in python? Learning Objectives: You should...

    Use of search structures! how can i make this program in python? Learning Objectives: You should consider and program an example of using search structures where you will evaluate search trees against hash tables. Given the three text files under containing the following: - A list of students (classes Student in the distributed code) - A list of marks obtained by the students (classes Exam results in the distributed code) - A list of topics (classes Subject in the distributed...

  • Hey I really need some help asap!!!! I have to take the node class that is...

    Hey I really need some help asap!!!! I have to take the node class that is in my ziplist file class and give it it's own file. My project has to have 4 file classes but have 3. The Node class is currently in the ziplist file. I need it to be in it's own file, but I am stuck on how to do this. I also need a uml diagram lab report explaining how I tested my code input...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT