Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

dynamic frame latest record

from datetime import date

rdd = sc.parallelize([
    [1, date(2016, 1, 7), 13.90],
    [1, date(2016, 1, 16), 14.50],
    [2, date(2016, 1, 9), 10.50],
    [2, date(2016, 1, 28), 5.50],
    [3, date(2016, 1, 5), 1.50]
])

df = rdd.toDF(['id','date','price'])
df.show()

+---+----------+-----+
| id|      date|price|
+---+----------+-----+
|  1|2016-01-07| 13.9|
|  1|2016-01-16| 14.5|
|  2|2016-01-09| 10.5|
|  2|2016-01-28|  5.5|
|  3|2016-01-05|  1.5|
+---+----------+-----+

df.registerTempTable("entries") // Replaced by createOrReplaceTempView in Spark 2.0

output = sqlContext.sql('''
    SELECT 
        *
    FROM (
        SELECT 
            *,
            dense_rank() OVER (PARTITION BY id ORDER BY date DESC) AS rank
        FROM entries
    ) vo WHERE rank = 1
''');

output.show();

+---+----------+-----+----+
| id|      date|price|rank|
+---+----------+-----+----+
|  1|2016-01-16| 14.5|   1|
|  2|2016-01-28|  5.5|   1|
|  3|2016-01-05|  1.5|   1|
+---+----------+-----+----+
Comment

PREVIOUS NEXT
Code Example
Python :: python graphviz undirected graph 
Python :: set list start at 1 python 
Python :: pythonpreventing an import from executing without call 
Python :: install formio data python library 
Python :: hashing in python using quadratic probing 
Python :: arabert 
Python :: use colabs gpu locally 
Python :: matplotlib text relative to axis 
Python :: i for i 
Python :: unittest sleep 
Python :: adding bootstrap grid dynamically django 
Python :: add multiple columns to dataframe if not exist pandas 
Python :: python string match http 
Python :: star psf 
Python :: documentation on fasttext gensim python 
Python :: com.codahale.metrics.annotation.timed 
Python :: lllll 
Python :: compute the average age for each gender? * 
Python :: python 3.7 release date 
Python :: python array of last n months 
Python :: Drip bucket limiter python 
Python :: scikit learn introduction 
Python :: repetition of word in python 
Python :: how to insert image in python 
Python :: split dataframe into multiple parts 
Python :: python lvl up 
Python :: python sns save plot lable axes 
Python :: how to count the appearance of number or string in a list python 
Python :: Python - Cómo cruda la cuerda 
Python :: softmax for nparray 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =