@danbihan

Forward fill NaNs

November 14, 2021 · Python
import pandas as pd
import numpy as np

clients = {
    'client_id' : [1001, 1001, 1001, 1002, 1002, 1002, 1003, 1003], 
    'ranking' : [1, 2, 3, 1, 2, 3, 1, 2],
    'value': [1000, np.nan, 1200, 1500, 1250, np.nan, 1100, np.nan]
}
clients_df = pd.DataFrame(clients)

Stemming

updated November 14, 2021 · Python
roots = ["cat", "bat", "rat", "kempt"]
sentence = "the unkempt cattle was rattled by the battery"

def replace_words(roots, sentence):
    words_list = sentence.split(' ')

    # Create a dictionary of words that can be stemmed
    stemming_dict = {
        word: stem 
        for word in words_list 

One element removed

November 14, 2021 · Python
list_x = [1,2,3,4,5]
list_y = [1,2,4,5]

def one_element_removed(list_x, list_y):
  
    difference = sum(list_x) - sum(list_y)
    return difference
    
print(one_element_removed(list_x, list_y))

Stop words

updated November 14, 2021 · Python
stopwords = [
    'I', 
    'as', 
    'to', 
    'you', 
    'your', 
    'but', 
    'be', 
    'a',
]

Stock profit

June 13, 2021 · Python
stock_prices = [10,5,20,32,25,12]
dts = [
  '2019-01-01', 
  '2019-01-02',
  '2019-01-03',
  '2019-01-04',
  '2019-01-05',
  '2019-01-06',
]

Bigrams

June 12, 2021 · Python
sentence = """
Have free hours and love children? 
Drive kids to school, soccer practice 
and other activities.
"""

def get_bigrams(sentence):
    words = sentence.split(' ')
    words = [word.replace('\n', '').lower() for word in words]