Skip to content

Instantly share code, notes, and snippets.

View bhishanpdl's full-sized avatar

Bhishan Poudel bhishanpdl

View GitHub Profile
@bhishanpdl
bhishanpdl / ipython_bold_and_blue.py
Created August 29, 2018 12:15
[ipython_bold_and_blue] #ipython
from IPython.display import Markdown, display
def printmd(string, color=None):
colorstr = "<span style='color:{}'>{}</span>".format(color, string)
display(Markdown(colorstr))
printmd("**bold and blue**", color="blue")
@bhishanpdl
bhishanpdl / bash_ls_basename.sh
Created August 29, 2018 12:16
[bash ls basename] #bash
ls dir1/dir2/*.txt | xargs -n 1 basename
@bhishanpdl
bhishanpdl / pandas_numeric.py
Created August 29, 2018 12:19
[pandas numeric] #numeric #pandas
.apply(pd.to_numeric,errors='coerce')
@bhishanpdl
bhishanpdl / subplot_eg1.py
Created August 29, 2018 12:20
[subplots] #plot
import numpy as np
import matplotlib.pyplot as plt
from numpy.random import random
t = np.linspace(0, 1, 101)
plt.figure()
plt.subplot(2, 2, 1); plt.hist(random(20))
plt.subplot(2, 2, 2); plt.plot(t, t**2, t, t**3 - t)
plt.subplot(2, 2, 3); plt.plot(random(20), random(20), 'r*')
@bhishanpdl
bhishanpdl / clear_plot.py
Created August 29, 2018 12:21
[clear plot] #plot #matplotlib
t = np.linspace(0, 5*np.pi, 501) # Define parameter for parametric plot.
plt.plot(t, np.sin(t)**2) # Generate plot.
plt.plot(t, np.sin(t)**3) # Create new plot on same axes.
ax = plt.gca() # Gain control of axes.
ax.hold(False) # Turn hold off.
plt.plot(t, np.sin(t)**4) # Create new plot; old data erased.
@bhishanpdl
bhishanpdl / pandas_plot_pivot.py
Created August 31, 2018 20:17
Pandas plotting using pivot table.
data = [[2000, 2000, 2000, 2001, 2001, 2001, 2002, 2002, 2002],
['Jan', 'Feb', 'Mar', 'Jan', 'Feb', 'Mar', 'Jan', 'Feb', 'Mar'],
[1, 2, 3, 4, 5, 6, 7, 8, 9]]
rows = list(zip(data[0], data[1], data[2]))
headers = ['Year', 'Month', 'Value']
df = pd.DataFrame(rows, columns=headers)
pivot_df = df.pivot(index='Year', columns='Month', values='Value')
@bhishanpdl
bhishanpdl / pandas_plot.py
Created August 31, 2018 20:19
Plotting with pandas
df.set_index('First_col').T.plot(kind='bar', stacked=True,figsize=(16,8))
@bhishanpdl
bhishanpdl / barplot_mean.py
Created August 31, 2018 20:40
Plot vertical bar plot with mean line.
%matplotlib inline
from pandas import Series
import matplotlib.pyplot as plt
heights = Series(
[165, 170, 195, 190, 170,
170, 185, 160, 170, 165,
185, 195, 185, 195, 200,
195, 185, 180, 185, 195],
import pandas as pd
import string
df = pd.DataFrame({'c0':range(1,10), 'c1': list(string.ascii_letters[0:9])})
df2 = df.iloc[[0, -1]]
# first and last 10
df = pd.concat([df.head(10), df.tail(10)])
import pandas as pd
import calendar
# Ref: https://docs.python.org/2/library/datetime.html
df['release_year'] = pd.to_datetime(df['release_date'], format='%m/%d/%y')
df['release_month'] = df['release_year'].dt.month