#!/usr/bin/env python # coding: utf-8 # # pandas Data # # > Marcos Duarte # > Laboratory of Biomechanics and Motor Control ([http://demotu.org/](http://demotu.org/)) # > Federal University of ABC, Brazil # In[1]: import numpy as np import pandas as pd get_ipython().run_line_magic('matplotlib', 'notebook') # tk qt notebook inline ipympl import matplotlib as mpl import matplotlib.pyplot as plt import sys, os sys.path.insert(1, r'./../functions') # In[2]: path2 = r'./../../../X/Clau/' name = 'WBDS01walkT06mkr.txt' fname = os.path.join(path2, name) # ## pandas with one index # In[3]: df = pd.read_csv(fname, sep='\t', header=0, index_col=0, dtype=np.float64, engine='c') df.columns = df.columns.str.replace('\.', '') df.head() # In[4]: ax = df.plot(y='RASISX', figsize=(8, 3), title='A plot of kinematics') ax.set_ylabel('Position [mm]') plt.tight_layout(pad=0, h_pad=0, rect=[0, 0, 1, .95]) # In[5]: def plot_widget(df): """general plot widget of a pandas dataframe """ from ipywidgets import widgets col_w = widgets.SelectMultiple(options=df.columns, value=[df.columns[0]], description='Column') clear_w = widgets.Checkbox(value=True, description='Clear axis') container = widgets.HBox(children=[col_w, clear_w]) display(container) fig, ax = plt.subplots(1, 1, figsize=(9, 4)) if col_w.value: df.plot(y=col_w.value[0], ax=ax) plt.tight_layout() plt.show() def plot(change): if clear_w.value: ax.clear() for c in col_w.value: df.plot(y=c, ax=ax) col_w.observe(plot, names='value') # In[6]: plot_widget(df) # ## pandas multiindex # # Data with hierarchical column index ([multiindex](http://pandas.pydata.org/pandas-docs/stable/advanced.html#creating-a-multiindex-hierarchical-index-object)) where columns have multiple levels. # In[7]: df = pd.read_csv(fname, sep='\t', header=0, index_col=0, dtype=np.float64, engine='c') # format columns as multindexes and relabel them cols = [s[:-1] for s in df.columns.str.replace('\.', '')] df.columns = [cols, list('XYZ')*int(df.shape[1]/3)] df.columns.set_names(names=['Marker', 'Coordinate'], level=[0, 1], inplace=True) #df = df.swaplevel(0, 1, axis=1) # for 'Coordinate' to go first # In[8]: df.head() # In[9]: df['RASIS'].head() # df.RASIS.head() # In[10]: df.RASIS.X.head() # df['RASIS']['X'].head() # In[12]: df.xs('X', level='Coordinate', axis=1).head() # In[13]: df.loc[:, (slice(None), 'X')].head() # In[14]: df.swaplevel(0, 1, axis=1).head() # for 'Coordinate' to go first # In[ ]: # In[ ]: # In[11]: ax = df.plot(y=('RASIS', 'X'), subplots=True, figsize=(8, 2), rot=0) # In[12]: ax = df.plot(y='RASIS', subplots=True, sharex=True, figsize=(8, 4), rot=0, title='A plot of kinematics') plt.tight_layout(pad=0, h_pad=0, rect=[0, 0, 1, .95]) # In[13]: values = df.reset_index(drop=False).values values[0, :5] # In[14]: df.head() # In[15]: x = df.swaplevel(0, 1, axis=1) # In[16]: x2 = x.unstack(level=-1) x2.head() # In[17]: x.head() # In[ ]: