Skip to content

Instantly share code, notes, and snippets.

@sshleifer
Last active May 14, 2016 22:49
Show Gist options
  • Save sshleifer/8084984756027ad77931cfc303eb2573 to your computer and use it in GitHub Desktop.
Save sshleifer/8084984756027ad77931cfc303eb2573 to your computer and use it in GitHub Desktop.
Notes on http://tomaugspurger.github.io/ Modern Pandas blogposts

Will immediately Incorporate

  • df.assign(lambda x: x.px * 2) # x is the DataFrame magically this will save us mucho code
  • df.loc[df.index.get_level_values(1) == 'donger'] can be df.loc[pd.IndexSlice[:,'donger'],]
  • ser.sort_values(ascending=False).head() can be ser.nlargest(5). nsmallest also exists.
  • df.add_suffix is built into pandas
  • df.dropna(thresh=4) If at least thresh items are missing, the row is dropped.

Could be useful

  • pd.TimeGrouper('H')
  • ser.str.extract # the str namespace is super rich and I underuse it
  • gb.rolling.agg(func) namespace # its a method!
  • df.select_dtypes(include=[np.number]) vs. Vadym's df.convert_objects(convert_numeric=True))
  • X = pd.get_dummies(X, drop_first=True)
  • df.fillna(value=df.median())

Seaborn

g = sns.FacetGrid(df, row='cut', aspect=4, size=1.76, margin_titles=True)
g.map(sns.kdeplot, 'price', shade=True, color='k')
  • sns.countplot basically same as sns.barplot but sorted.
  • sns.pairplot(df, hue=hue)
  • Type Loggers
  • lowess line #

Issue: sending func to rename does not work how I expect on MultiIndex

>>> df = pd.DataFrame({'x1': [1,2,3], 'x': [4,5,6]})
>>> df.columns= pd.MultiIndex.from_tuples([('a', x) for x in df.columns])
>>> df.columns.map(lambda x: '_'.join(x))  # this is the result I expect  
=>  ['a_x1' 'a_x2']
>>> df.rename(columns=lambda x: '_'.join(x)).columns.tolist() # this only accesses level 1
=> [('a', 'x_1'), ('a', 'x_2')]
>>> df.rename_axis(lambda x: '_'.join(x), axis=1).columns.tolist()  # similarly ineffectual
=> [('a', 'x_1'), ('a', 'x_2')]

Read

To Read:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment