- Radovan Kavický
- President & Principal Data Scientist, GapData Institute
- @radovankavicky
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| from subprocess import check_call | |
| def post_save(model, os_path, contents_manager): | |
| """post-save hook for converting notebooks to .py and .html files.""" | |
| if model['type'] != 'notebook': | |
| return # only do this for notebooks | |
| d, fname = os.path.split(os_path) | |
| check_call(['jupyter', 'nbconvert', '--to', 'script', fname], cwd=d) | |
| check_call(['jupyter', 'nbconvert', '--to', 'html', fname], cwd=d) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(idbr) # devtools::install_github('walkerke/idbr') | |
| library(ggplot2) | |
| library(animation) | |
| library(dplyr) | |
| library(ggthemes) | |
| idb_api_key("Your Census API key goes here") | |
| male <- idb1('JA', 2010:2050, sex = 'male') %>% | |
| mutate(POP = POP * -1, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyStreamListener(tweepy.StreamListener): | |
| def __init__(self, api=None): | |
| super(MyStreamListener, self).__init__() | |
| self.num_tweets = 0 | |
| self.file = open("tweets.txt", "w") | |
| def on_status(self, status): | |
| tweet = status._json | |
| self.file.write( json.dumps(tweet) + '\n' ) | |
| self.num_tweets += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(httr) | |
| library(magick) | |
| library(hrbrthemes) | |
| library(ggplot2) | |
| theme_tweet_rc <- function(grid = "XY", style = c("stream", "card"), retina=FALSE) { | |
| style <- match.arg(tolower(style), c("stream", "card")) | |
| switch( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # List unique values in a DataFrame column | |
| # h/t @makmanalp for the updated syntax! | |
| df['Column Name'].unique() | |
| # Convert Series datatype to numeric (will error if column has non-numeric values) | |
| # h/t @makmanalp | |
| pd.to_numeric(df['Column Name']) | |
| # Convert Series datatype to numeric, changing non-numeric values to NaN | |
| # h/t @makmanalp for the updated syntax! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(ggplot2) | |
| library(rbokeh) | |
| library(htmlwidgets) | |
| structure(list(wk = structure(c(16069, 16237, 16244, 16251, 16279, | |
| 16286, 16300, 16307, 16314, 16321, 16328, 16335, 16342, 16349, | |
| 16356, 16363, 16377, 16384, 16391, 16398, 16412, 16419, 16426, | |
| 16440, 16447, 16454, 16468, 16475, 16496, 16503, 16510, 16517, | |
| 16524, 16538, 16552, 16559, 16566, 16573), class = "Date"), n = c(1L, | |
| 1L, 1L, 1L, 3L, 1L, 3L, 2L, 4L, 2L, 3L, 2L, 5L, 5L, 1L, 1L, 3L, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import rpy2.robjects as robjects | |
| import pandas.rpy.common as com | |
| import pandas as pd | |
| ## load .RData and converts to pd.DataFrame | |
| robj = robjects.r.load('test.RData') | |
| # iterate over datasets the file | |
| for sets in robj: | |
| myRData = com.load_data(sets) | |
| # convert to DataFrame |
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 1 column, instead of 2 in line 1.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Number of deaths;XY coordinates | |
| 3;-0.13793,51.513418 | |
| 2;-0.137883,51.513361 | |
| 1;-0.137853,51.513317 | |
| 1;-0.137812,51.513262 | |
| 4;-0.137767,51.513204 | |
| 2;-0.137537,51.513184 | |
| 2;-0.1382,51.513359 | |
| 2;-0.138045,51.513328 | |
| 3;-0.138276,51.513323 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Number of deaths | X coordinate | Y coordinate | |
|---|---|---|---|
| 3 | -0.13793 | 51.513418 | |
| 2 | -0.137883 | 51.513361 | |
| 1 | -0.137853 | 51.513317 | |
| 1 | -0.137812 | 51.513262 | |
| 4 | -0.137767 | 51.513204 | |
| 2 | -0.137537 | 51.513184 | |
| 2 | -0.1382 | 51.513359 | |
| 2 | -0.138045 | 51.513328 | |
| 3 | -0.138276 | 51.513323 |