1/23/19 (Intro to Python pt.3-5)

This document contains both Python and R code and is made in R Markdown

Python - Pandas

import pandas
data = pandas.read_csv('Twitter-sentiment-self-drive-DFE.csv', encoding = "ISO-8859-1")

R - Display pandas dataframe

datatable(py$data, options = list(pageLength = 2))

Py - Columns

print(data.columns)
## Index([u'_unit_id', u'_golden', u'_unit_state', u'_trusted_judgments',
##        u'_last_judgment_at', u'sentiment', u'sentiment:confidence', u'our_id',
##        u'sentiment_gold', u'sentiment_gold_reason', u'text'],
##       dtype='object')

R - Columns

colnames(py$data)
##  [1] "_unit_id"              "_golden"              
##  [3] "_unit_state"           "_trusted_judgments"   
##  [5] "_last_judgment_at"     "sentiment"            
##  [7] "sentiment:confidence"  "our_id"               
##  [9] "sentiment_gold"        "sentiment_gold_reason"
## [11] "text"

Py - Indexing

print(data['sentiment'].head())
## 0    5
## 1    5
## 2    2
## 3    2
## 4    3
## Name: sentiment, dtype: object

R - Indexing

head(py$data$sentiment)
## [1] "5" "5" "2" "2" "3" "3"

Can also write it like this:

head(py$data[,"sentiment"])
## [1] "5" "5" "2" "2" "3" "3"

Markdown Example

\(f(x) = mx + b\)

Let’s put in some data

\(1 = m*5 + b\)

\(-1 = m*1 + b\)

solving for \(m\) and \(b\) gives me:

\(m = \frac{1}{2}\)

\(b = \frac{-3}{2}\)

End note:

Can’t use shiny+Python, but parametrized reports work! Can take user inputs and have a button to compile document with parameters, which would mean it takes user inputs and works with R and Python combined.

print(r.params["parameterExample"])
## This is the output from the parameter example!