Hi, I have tried using NumPy and my code gets 3 out
of 4 test to work. I can not figure out how to associate the
columns with the titles. I understand that NumPy array will not
handle multiple data types so, I have converted the array into what
I thought was a structured array by transposing the array so that
the columns have all the same data types and the converting it to
tuples. Do you have a solution?
Question below:
The NumPy array you created from task 1 is unstructured because
we let NumPy decide what the datatype for each value should be.
Also, it contains the header row that is not necessary for the
analysis. Typically, it contains float values, with some
description columns like created_at etc. So, we are going to remove
the header row, and we are also going to explicitly tell NumPy to
convert all columns to type float (i.e., "float") apart from
columns specified by indexes, which should be Unicode of length 30
characters (i.e., "<U30"). Finally, every row is converted as a
type tuple (e.g., tuple(i) for i in data).
Write a
function unstructured_to_structured(data,
indexes) that achieves the above goal.
These are the results from the tests:
My code below:
def unstructured_to_structured(data, indexes):
"""remove the header row and convert all columns
to floats"""
data = np.delete(data,0,0)
size = np.size(data,1)
data = np.transpose(data)
data = list([list(i) for i in data])
counter = 0
while counter < size:
if counter in indexes:
i=0
while i <
len(data[counter]):
data[counter] = np.array(data[counter],dtype= '<U30')
i+=1
else:
i=0
while i <
len(data[counter]):
data[counter] = np.array(data[counter],dtype= 'float')
i+=1
counter += 1
data = [list(i) for i in zip(*data)]
data = tuple([tuple(i) for i in data])
return data
Sample input data below from a csv file:
Hi, I have tried using NumPy and my code gets 3 out of 4 test to work. I can not figure out how to associate the column
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am