SCRIPTS PANDAS basicΒΆ

# -*-Python-*-
# Created by bgrierson at 09 Jan 2017  15:37

# Pandas (pandas.pydata.org) is a data analysis library that operates on structured arrays.

# A Pandas Series is one fundamental building block in Pandas
# a series can be just a list of numbers, and will have an associated index
# The default index is
s = pandas.Series([1, 2, 3])
printi('Series s without index')
print(s)

# We can give each number an index, as follows
s = pandas.Series([1, 2, 3], ['a', 'b', 'c'])
printi('Series s with index a,b,c')
print(s)

# We can refer to an entry like and array or with its index like a dictionary
printi("s[0] and s['a']")
print(s[0])
print(s['a'])

# You can also give the series a name similar to a NetCDF
s.name = 'something'
printi('s with name')
print(s)

# You can get the numbers (x=index, and y=values) back out with
x = s.index.values
y = s.values

# A 1d of numbers, such as a time history, with its name can be formed and plotted easily
t = linspace(0, 1, 101)
w = 4.0 * np.pi
vals = np.sin(w * t)

s = pandas.Series(vals, index=t, name='sin(w*t)')
fig, ax = plt.subplots()
s.plot(color='g', ax=ax)
ax.set_title('Simple pandas.Series')
ax.legend()

# Beyond a Series, we create a DataFrame, which is a number of pandas Series that have
# a common axis.  This can be thought of as a number of measurements all with the same
# time axis, or a SQL database, or spreadsheet, etc...
# Here we create a pandas DataFrame from multiple pandas Series
t = linspace(0, 1, 101)
f1 = np.sin(2.0 * np.pi * 4.0 * t)
f2 = np.sin(2.0 * np.pi * 8.0 * t)
s1 = pandas.Series(f1, index=t)
s2 = pandas.Series(f2, index=t)
df = pandas.DataFrame({'sin 1': s1, 'sin 2': s2})
print(df)
fig, ax = plt.subplots()
df.plot(ax=ax)