Skip to content

Instantly share code, notes, and snippets.

@deparkes
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deparkes/d76b093a034d496ea196 to your computer and use it in GitHub Desktop.
Save deparkes/d76b093a034d496ea196 to your computer and use it in GitHub Desktop.
Pandas for physics - a few key steps
# -*- coding: utf-8 -*-
import pandas as pd
import matplotlib.pyplot as plt
import math
# Some functions to later apply to our columns
def dcos(theta):
theta = theta*(math.pi/180)
return math.cos(theta)
def dcos2(theta):
theta = theta*(math.pi/180)
return math.cos(2*theta)
# Load csv
# http://pandas.pydata.org/pandas-docs/dev/io.html
mydata = pd.read_csv('data.dat')
# Display columns
# head() shows just the first 5 rows
print(mydata.head())
# Display a summary of information about the data including mean, std, min, max, etc.
print(mydata.describe())
# We can also plot columns separately
# http://stackoverflow.com/questions/17812978/how-to-plot-two-columns-of-a-pandas-data-frame-using-points
fig = plt.figure()
mydata.plot(x='Theta', y='sin(theta)', style='-')
# Add new column to data frame
# http://synesthesiam.com/posts/an-introduction-to-pandas.html#bulk-operations-with-apply
mydata['cos(theta)'] = pd.Series(mydata["Theta"].apply(dcos), index=mydata.index)
ax = mydata.plot(x='Theta', y='cos(theta)', style='-')
plt.title("Panda Plotting")
# Add new column to data frame
mydata['cos2(theta)'] = pd.Series(mydata["Theta"].apply(dcos2), index=mydata.index)
mydata.plot(x='Theta', y='cos2(theta)', style='-')
# Having played around with our data and want to save a copy of it
# Pandas lets us do this quite easily.
# http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.to_csv.html
mydata.to_csv('Newcsv.dat')
Theta,sin(theta)
0,0
5,0.0871557427
10,0.1736481777
15,0.2588190451
20,0.3420201433
25,0.4226182617
30,0.5
35,0.5735764364
40,0.6427876097
45,0.7071067812
50,0.7660444431
55,0.8191520443
60,0.8660254038
65,0.906307787
70,0.9396926208
75,0.9659258263
80,0.984807753
85,0.9961946981
90,1
95,0.9961946981
100,0.984807753
105,0.9659258263
110,0.9396926208
115,0.906307787
120,0.8660254038
125,0.8191520443
130,0.7660444431
135,0.7071067812
140,0.6427876097
145,0.5735764364
150,0.5
155,0.4226182617
160,0.3420201433
165,0.2588190451
170,0.1736481777
175,0.0871557427
180,1.22460635382238E-016
185,-0.0871557427
190,-0.1736481777
195,-0.2588190451
200,-0.3420201433
205,-0.4226182617
210,-0.5
215,-0.5735764364
220,-0.6427876097
225,-0.7071067812
230,-0.7660444431
235,-0.8191520443
240,-0.8660254038
245,-0.906307787
250,-0.9396926208
255,-0.9659258263
260,-0.984807753
265,-0.9961946981
270,-1
275,-0.9961946981
280,-0.984807753
285,-0.9659258263
290,-0.9396926208
295,-0.906307787
300,-0.8660254038
305,-0.8191520443
310,-0.7660444431
315,-0.7071067812
320,-0.6427876097
325,-0.5735764364
330,-0.5
335,-0.4226182617
340,-0.3420201433
345,-0.2588190451
350,-0.1736481777
355,-0.0871557427
360,-2.44921270764475E-016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment