Phys 238 : Reading .CSV Files With Mathematica

Getting Started

Mathematica remembers definitions, even if you delete them from the notebook.  This can sometimes be confusing, so it’s often a good idea to simply clear everything and start anew.  This is especially true if you use the Evaluation -> Evaluate Notebook menu item.

Clear["Global`*"] (* Clear all variable and function definitions *)

Reading a CSV file

It is not always obvious where Mathematica will look for data files or export graphs.  It is often useful to keep your data in the same directory as the notebook.  The following command sets the default directory to be the same as the directory for this notebook.

SetDirectory[NotebookDirectory[]]

Mathematica has many different ways to read and interpret .csv files.   One useful way is to Import the data into what Mathematica calls a “Dataset”, which is a structured object with it’s own special query language.  Here’s how it works for the pendulum data.

fulldata = Import["pendulum-20250129.csv", "Dataset"]

To extract the period data, we want to pick {All rows, 3rd column}, but we can also refer to the column by name:

data = fulldata[All, "Latest: Period (s)"]

For this particular data set, this still has the problem that most of the rows are empty.  It is possible to filter them out while importing, but a more generally useful approach is to use the Select[ ] function to select only data elements that are numbers.  (Select can also be used for much more complex testing, but this is sufficient for here.)

data = Select[data, NumberQ]

Summary of Import:

For this data set,  here are the relevant commands,

SetDirectory[NotebookDirectory[]]

fulldata = Import["pendulum-20250129.csv", "Dataset"];

data = Select[fulldata[All, "Latest: Period (s)"], NumberQ];

(Optional) Save the period data for later importing.

Export["pendulum-20250129-period.csv", data]

Statistical Analysis

npts = Length[data]

T = Mean[data]

σT = StandardDeviation[data]

δT = σT/Sqrt[npts]

Mathematica can express this as a value with uncertainty using the Around[ ] function:

Around[T, δT]

Tplot = ListPlot[data, LabelStyle->Larger, Frame -> True, FrameLabel -> {"Cycle", "Period (s)"}, ImageSize -> Scaled[0.8]]

Further explorations

We can illustrate the ±1 σ range on the plot:

TCompPlot = Show[{Tplot,
        Plot[{T + σT, T, T - σT}, {cycle, 1, Length[data]}, PlotLegends -> {"+1 σ", "Mean", "-1 σ"}]
}]

Export["pendulum-period-plot.pdf", TCompPlot]

We can also count the numbers with a particular range with the Select[ ]  function.  This function steps through the ‘data’ array and assigns the special symbol ‘#’ to the current element of the array.  (The ‘&’ must also be included at the end of the comparison.  See the ‘Pure Functions’ entry in the on-line help for more information.)

within1 = Select[data, T - σT <= # <= T + σT &];
100. * Length[within1]/npts

within2 = Select[data, T - 2σT <= # <= T +2 σT &];
100. * Length[within2]/npts

within3 = Select[data, T - 3σT <= # <= T +3 σT &];
100. * Length[within3]/npts