Skip to content

Instantly share code, notes, and snippets.

@shravan-kuchkula
shravan-kuchkula / generateWordCloud.R
Last active March 31, 2017 08:57
Create a word cloud from a list of tweets.
# Takes a list of status/twitter objects, extracts the text,
# cleans the text, calculates word frequencies and generates
# a word cloud.
generateWordCloud <- function(tweets){
#Get the text from the status/twitter object
tweets_list <- sapply(tweets, function(x) x$getText())
#Remove any weird symbols from the text
tweets_list <- str_replace_all(tweets_list, "[^[:graph:]]", " ")
@shravan-kuchkula
shravan-kuchkula / installRequiredPackages.R
Created March 31, 2017 08:54
Install and load packages in R
# Install a package and load it.
installRequiredPackages <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[,"Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
libs <- c("readr", "dplyr", "tidyr", "ggplot2",
"magrittr", "markdown", "knitr", "Hmisc",
@shravan-kuchkula
shravan-kuchkula / forLoopInR.R
Created March 31, 2017 18:16
A safer way to write a for loop in R. Use seq_along() to handle an empty data frame. Useful when using a for loop within a function.
df <- data.frame(
a = rnorm(10),
b = rnorm(10),
c = rnorm(10),
d = rnorm(10)
)
# Replace the 1:ncol(df) sequence
for (i in seq_along(df)) {
print(median(df[[i]]))
@shravan-kuchkula
shravan-kuchkula / lists2dict.py
Created April 19, 2017 01:06
Create a dictionary from 2 lists, one contains the keys, the other contains the values. (Python)
# Define lists2dict()
def lists2dict(list1, list2):
"""Return a dictionary where list1 provides
the keys and list2 provides the values."""
# Zip lists: zipped_lists
zipped_lists = zip(list1, list2)
# Create a dictionary: rs_dict
rs_dict = dict(zipped_lists)
@shravan-kuchkula
shravan-kuchkula / dictionaryToDataframe.py
Last active April 19, 2017 14:47
Create a pandas dataframe from a list of dictionaries
# Import the pandas package
import pandas as pd
# Turn list of lists into list of dicts: list_of_dicts
# https://gist.github.com/shravan-kuchkula/6ca3054d5ec0549e1759d739b3d47513
list_of_dicts = [lists2dict(feature_names, sublist) for sublist in row_lists]
# Turn list of dicts into a dataframe: df
df = pd.DataFrame(list_of_dicts)
@shravan-kuchkula
shravan-kuchkula / customer.xml
Created July 26, 2017 19:08
XML Mini project
<?xml version="1.0" encoding="UTF-8"?>
<TLSales xmlns="http://tlsales.com/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tlsales.com/namespace /tmp/msds7330/customer.xsd">
<Address>
<Name>Addr1</Name>
<Street>123 Main St.</Street>
<City>Seattle</City>
</Address>
<Customer OneTime="true" Regular="false" Senior="false" Discount="5">
<First>John Q.</First>
<Last>Public</Last>
@shravan-kuchkula
shravan-kuchkula / mongoUpdate.js
Last active August 28, 2017 03:34
Update operation from mongo shell. All 4 cases
# To insert the data in the people collection use 'insertOne.py' that I have given earlier
# Display the contents of the people collection
> db.people.find()
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b6"), "name" : "Smith", "age" : 30, "profession" : "hacker" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b7"), "name" : "Jones", "age" : 35, "profession" : "baker" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b8"), "name" : "Alice" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1b9"), "name" : "Bob" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1ba"), "name" : "Charlie" }
{ "_id" : ObjectId("59a36310fc2b2b3d843af1bb"), "name" : "Dave" }
@shravan-kuchkula
shravan-kuchkula / insertOne.py
Created August 28, 2017 03:25
A simple pymongo script to create the people collection
import pymongo
connection = pymongo.MongoClient("mongodb://localhost")
db = connection.school
people = db.people
people.drop()
def insert(peopleList):
@shravan-kuchkula
shravan-kuchkula / removeSpacesFromFileNames.py
Created February 9, 2018 03:07
remove spaces from filenames inside a directory using a python script
import os
files = os.listdir(os.getcwd())
[os.replace(file, file.replace(" ", "_")) for file in files]
Conda is a powerful package manager and environment manager that you use with command line commands at the Anaconda Prompt for Windows, or in a Terminal window for macOS or Linux.
Common Commands:
a. To see a list of all your environments, type:
conda info --envs
$conda info --envs
# conda environments:
#
base * /Users/Shravan/anaconda3