How to insert np.array values into a new csv file

Hi team,

I have just finished the NumPy lesson in the Data Science Path. I have a question from doing Betty’s Bakery Project. I did it well overall, but I need to know how to insert the values from a np.array into a new CSV file?

In the exercise (no.9), we have an array of grocery list (Flour, Sugar, Eggs, Milk, Butter) from this code:

cookies = recipes[2,:]
print(cookies)
# [ 2.75  1.5   1.    0.    1.  ]

double_batch_cupcakes = cupcakes * 2
print(double_batch_cupcakes)
#[ 4.   1.5  4.   2.   1. ]

grocery_list = double_batch_cupcakes + cookies
print(grocery_list)
#[ 6.75  3.    5.    2.    2.  ]

So this grocery list is the number of ingredients that Betty needs to buy to make a double batch of cupcakes and a single batch of cookies. Now from this point, how do I enter this number in a CSV file or a data frame so that Betty can print it and take it with her when she goes to the supermarket :slight_smile: ?

Will appreciate any pointers I can get.

Regards,
Jimmy

Hi Jimmy,
Good question. You can import the savetxt function and use the savetxt like so:
from numpy import savetxt
savetxt('data.csv', data, delimeter=',')

check this out:
https://machinelearningmastery.com/how-to-save-a-numpy-array-to-file-for-machine-learning/#:~:text=You%20can%20save%20your%20NumPy,file%2C%20most%20commonly%20a%20comma.

3 Likes

Hey Lisa,

Thanks so much for this…

Regards,
Jimmy