This is part of a series on data storage with Python.
In this post, we'll be covering .csv files. CSV stands for comma-separated values. If you've ever worked with spreadsheets, you know what these are.
CSV files are ideal for saving data that you may have taken by hand and put in a table in your lab notebook. They support basic information for headers, and then inferred typing for columns of data. Some people like to stick YAML metadata as header data before the csv file begins, but this isn't standard.
You can write a CSV file using the Pandas Library as follows
import pandas as pd
import numpy as np
# Generate some dummy data
timestamp = np.arange(10) * 10
voltage = np.random.randn(10) * .1 + 1
temperature = np.random.randn(10) * .1 + 25
# Store the data as a Pandas DataFrame
df = pd.DataFrame({
'voltage (V)': voltage,
'temperature (degC)': temperature
}, index=timestamp)
df.index.name = 'timestamp (s)'
# Write it to a CSV file
df.to_csv('somefile.csv')
Which makes a file that looks like
timestamp (s),voltage (V),temperature (degC)
0,0.9769016029735853,25.057742007399924
10,1.0910958340447057,25.021318133625844
20,1.0124794367682834,25.171720035243958
30,1.0187734751103554,24.87188798581284
40,1.0365117402005928,25.101936428565686
50,1.04180480160457,24.93160754439336
60,1.0324176978016553,25.15939557413849
70,1.131506487313099,24.942521207832005
80,1.0270401680229668,24.899996596511734
90,0.8663463019236542,25.07189500764065
And read it back with
import pandas as pd
df = pd.read_csv('somefile.csv', index_col=0)
CSV files work great when you need to save some data that you took by hand. However, for larger data sets, you should consider another data format that saves data in binary format instead of ascii format.