Skip to content

Instantly share code, notes, and snippets.

View calendarGraph5
# set colors
f, ax = plt.subplots(figsize=(6, 18))
# drop duplicates for bookDf **End of book A is the start of book B
df = bookDf.copy()
View calendarGraph4
# Adding needed columns
bookDf['DateOrig'] = bookDf['Date']
bookDf['Day'] = bookDf['Date'].apply(lambda x:
bookDf['Month'] = bookDf['Date'].apply(lambda x: dt.datetime.strftime(x,'%b'))
bookDf['DOW'] = bookDf['Date'].apply(lambda x: dt.datetime.strftime(x,'%a'))
bookDf['Month_num'] = bookDf['Date'].apply(lambda x: x.month)
bookDf['DOW_num'] = bookDf['Date'].apply(lambda x: x.weekday())
bookDf['Week_num'] = bookDf['Date'].apply(lambda x: int(dt.datetime.strftime(x,'%W')))
#add proxy for different colours
View calendarGraph3
# create a df with date from 1st to last day of year from min_year
dateList = pd.DataFrame(list(date_generator(dt.datetime(year,1,1,0,0,0),dt.datetime(year,12,31,0,0,0))),columns=['Date'])
dateList.Date = dateList.Date.astype('O')
View calendarGraph2
def date_generator(from_date,to_date):
while from_date<=to_date:
yield from_date
from_date = from_date + dt.timedelta(days=1)
# create a new df with 2 columns
# col1 : Title, col2: DateRead
Title = []
Date = []
for index,row in booksv2.iterrows():
View calendarGraph1
import pandas as pd
import datetime as dt
booksv2 = pd.read_csv('book1.csv')
booksv2['Start'] = booksv2['Start'].apply(lambda x: dt.datetime.strptime(str(x),'%d/%m/%Y'))
booksv2['End'] = booksv2['End'].apply(lambda x: dt.datetime.strptime(str(x),'%d/%m/%Y'))
min_date = min(list(booksv2['Start'])+list(booksv2['End']))
max_date = max(list(booksv2['Start'])+list(booksv2['End']))
#year = min_date.year
ken333135 / Create a Sankey Diagram
Created May 8, 2019 08:08
Script to create a Sankey Diagram using genSankey
View Create a Sankey Diagram
import pandas as pd
import plotly
import plotly.plotly as py
fig = genSankey(df,cat_cols=['lvl1','lvl2','lvl3','lvl4'],value_cols='count',title='Word Etymology')
plotly.offline.plot(fig, validate=False)
ken333135 / genSankey
Created May 8, 2019 07:57
Wrapper Function to create Sankey Diagram from DataFrame
View genSankey
def genSankey(df,cat_cols=[],value_cols='',title='Sankey Diagram'):
# maximum of 6 value cols -> 6 colors
colorPalette = ['#4B8BBE','#306998','#FFE873','#FFD43B','#646464']
labelList = []
colorNumList = []
for catCol in cat_cols:
labelListTemp = list(set(df[catCol].values))
labelList = labelList + labelListTemp
View DrawGraphDash 2
app.layout = html.Div([
html.Div(className='row', children=[
html.Div([html.H2('Overall Data'),
html.P('Num of nodes: ' + str(len(G.nodes))),
html.P('Num of edges: ' + str(len(G.edges)))],
className='three columns'),
html.H2('Selected Data'),
View DrawGraphDash 1
fig = go.Figure(data=[edge_trace, node_trace],
title='<br>Network Graph of '+str(num_nodes)+' rules',
annotations=[ dict(
xref="paper", yref="paper",
ken333135 / TopWords 4
Created June 19, 2018 02:38
Get Top words for each class (multi-class text classification)
View TopWords 4
top_words = []
#loop to find top 5 words of each class in the dataset
for code in vect_data.index:
#print the list of top words