Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Data Science Basics Describing Data Loading Raw Data

S B
S B
4,497 Points

Difference between def open_with_csv and def load_data functions?

Fantastic course so far! I've gone through all the videos in this section, and I've had to come back to this particular video to figure out WHEN to use the functions: 'def open_with_csv' vs. 'def load_data'.

For example: In video 2, number_of_ties calls the function number of records, which passes in the data_sample argument using data_from_csv = open_with_csv('data.csv')

However, just after that, number_of_ties_my_csv calls the function number_of_records2 which passes in the data_sample argument using my_csv = load_data('data.csv')

My question is simple, how do you know when to use 'open_with_csv' vs. the 'load_data' function?

Thanks!

2 Answers

The difference is how the data is imported and what is returned from the functions/methods.

open_with_csv creates an empty list and then reads the data and appends it to the list, then returns it. So it's a regular Python list object.

load_data on the other hand, uses numpy's genfromtxt method, and returns its own ndarray object.

So I guess you would use load_data if you wanted to perform numpy operations on the data, otherwise open_with_csv would be fine.

Sahar Nasiri
Sahar Nasiri
7,454 Points

She did not print the my_csv which is returned from load_data method! The list that you saw again is the same print of data_from_csv[0] (you can test this my make print(data_from_csv[0]) a comment) If you print my_csv you can see what is the result which by the way I have no idea what it is :)). Does anyone know what is this?

[ (0, 399795110, 119.83, b'Burberry London checked silk tie', 870, b'Burberry', b'http://bim.shopstyle.com/pim/3
c/27/3c27ee2884a1163917d41109fc3be1f3_best.jpg', b'Nude silk tie from Burberry London featuring a classic pointe
d design and a contrasting black and red house check print.', b'farfetch.com', b'_print', b'_silk')             
 (1, 12822215, 59.5, b'BB1 Repp Slim Tie', 3415, b'Brooks Brothers', b'http://bim.shopstyle.com/pim/6b/63/6b63cc
4838699797aac410ea0316faca_best.jpg', b'"Expertly woven in England, this collection is known for its stripes and
 vibrant colors. Our signature stripe detailing goes from right to left. Pure silk. 2 5/8"" width. Dry clean. Im
ported."', b'Brooks Brothers', b'_striped', b'_silk')                                                           
 (2, 369523836, 119.83, b'Burberry London check woven tie', 870, b'Burberry', b'http://bim.shopstyle.com/pim/51/
d0/51d0e2b9608115fb0ae74f00eec55457_best.jpg', b"Beige silk tie from Burberry London featuring a classic pointed
 design and a signature woven 'House' check.", b'farfetch.com', b'unknown', b'_silk')                           
 ...,                                                                                                           
 (5047, 459995357, 205.0, b'Ermenegildo Zegna Woven Silk Tie', 1139, b'Ermenegildo Zegna', b'http://bim.shopstyl
e.com/pim/d7/bf/d7bf5d7430bbe972653c151c9b50730e_best.jpg', b'"Bold, intricate paisley marks a rich tie cut from
 pure silk. Founded in northern Italy in 1910, Ermenegildo Zegna began as a woolen mill, which soon became known
 for producing beautiful, high-quality textiles\xe2\x80\x94identified then and now by a signature red seal. The 
company\xe2\x80\x99s expertise in quality materials persists to this day and can be found in every product they 
make. <ul> <li>3 1/2"" width; 57"" length.</li> <li>100% silk.</li> <li>Dry clean.</li> <li>By Ermenegildo Zegna
; made in Italy.</li> <li>Men\'s Furnishings.</li> </ul>"', b'Nordstrom', b'_paisley', b'_wool')
company\xe2\x80\x99s expertise in quality materials persists to this day and can be found in every product they 
make. <ul> <li>3 1/2"" width; 57"" length.</li> <li>100% silk.</li> <li>Dry clean.</li> <li>By Ermenegildo Zegna
; made in Italy.</li> <li>Men\'s Furnishings.</li> </ul>"', b'Nordstrom', b'_paisley', b'_wool')                
 (5048, 460937819, 65.0, b'David Donahue Silk & Cotton Bow Tie', 1041, b'David Donahue', b'http://bim.shopstyle.
com/pim/65/16/6516d892c18241ac59ba15f7dd1c525f_best.jpg', b"Complex stripes marks a textured tie cut from a blen
d of silk and cotton. <ul> <li>Untied; adjustable.</li> <li>60% silk, 40% cotton.</li> <li>Dry clean.</li> <li>B
y David Donahue; made in Italy.</li> <li>Men's Furnishings.</li> </ul>", b'Nordstrom', b'_striped', b'_cotton')