Skip to content

Instantly share code, notes, and snippets.

@benjaminrose
Last active June 2, 2021 23:24
Show Gist options
  • Save benjaminrose/6fc4025d871077793a8d to your computer and use it in GitHub Desktop.
Save benjaminrose/6fc4025d871077793a8d to your computer and use it in GitHub Desktop.
This is an useful example of how I like to save my data using numpy and savetxt.
import numpy as np
#create random data
data1 = np.random.rand(10)
data2 = np.random.rand(10)
data3 = np.random.rand(10)
#stack data into one ojbect
data_list = np.dstack((data1, data2, data3))
#set up file metadata
filename = 'data.csv'
header = '''Data from awsome science
data1, data2, data3
(cm), (ft), (lbs)''' #don't have tabs here, they will show up in the header.
#saving data
np.savetxt(filename, data_list[0], delimiter=',', header=header)
#data still needs one wraper around it, but after dstack it had an extra.
@benjaminrose
Copy link
Author

I should probably just use:

import pandas as pd

pf = pd.DataFrame(data.from.somewhere)

with open('test2.csv', 'w') as f:
    f.write('# This is my comment\n')
df.to_csv('test2.csv', mode='a')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment