2017-09-06 7 views
0

pysparkの2つのデータフレームの間で減算を実行したい。データフレームを減算しながらいくつかの列を無視しなければならないという課題があります。しかし、最後のデータフレームには、無視された列を含むすべての列が含まれている必要があります。ここでPySpark:一部の列を無視してデータフレームを減算する

は一例です:

期待
userLeft = sc.parallelize([ 
    Row(id=u'1', 
     first_name=u'Steve', 
     last_name=u'Kent', 
     email=u'[email protected]', 
     date1=u'2017-02-08'), 
    Row(id=u'2', 
     first_name=u'Margaret', 
     last_name=u'Peace', 
     email=u'[email protected]', 
     date1=u'2017-02-09'), 
    Row(id=u'3', 
     first_name=None, 
     last_name=u'hh', 
     email=u'[email protected]', 
     date1=u'2017-02-10') 
]).toDF() 

userRight = sc.parallelize([ 
    Row(id=u'2', 
     first_name=u'Margaret', 
     last_name=u'Peace', 
     email=u'[email protected]', 
     date1=u'2017-02-11'), 
    Row(id=u'3', 
     first_name=None, 
     last_name=u'hh', 
     email=u'[email protected]', 
     date1=u'2017-02-12') 
]).toDF() 

ActiveDF = userLeft.subtract(userRight) ||| Ignore "date1" column while subtracting. 

最終結果は "日付1" の列を含む次のようになります。

+----------+--------------------+----------+---+---------+ 
|  date1|    email|first_name| id|last_name| 
+----------+--------------------+----------+---+---------+ 
|2017-02-08| [email protected]|  Steve| 1|  Kent| 
+----------+--------------------+----------+---+---------+ 

答えて

1

あなたがanti-joinを必要とするようだ:

userLeft.join(userRight, ["id"], "leftanti").show() 
+----------+----------------+----------+---+---------+ 
|  date1|   email|first_name| id|last_name| 
+----------+----------------+----------+---+---------+ 
|2017-02-08|[email protected]|  Steve| 1|  Kent| 
+----------+----------------+----------+---+---------+ 
+0

はpyspark 1.6では利用できません。私はこれらのデータフレームのための特定の主キーを持っていません。私のデータフレームは実行時に生成されます。だから、私は列の詳細を知らない。しかし、私はいつも知っている、私は参加している間考慮したくないコラム。 – orNehPraka

+0

'date1'を除くすべての列に参加したい場合、' userLeft.join(userRight、userLeft、[col!for userLeft.columns in col!= 'date1']、 "leftanti")オプションがあります。 nullでない場合は、空の文字列でnull値を入力する必要があります。 – Psidom

0

ます。またfull joinを使用してのみnull値に保つことができます。

userLeft.join(
    userRight, 
    [c for c in userLeft.columns if c != "date1"], 
    "full" 
).filter(psf.isnull(userLeft.date1) | psf.isnull(userRight.date1)).show() 

    +------------------+----------+---+---------+----------+----------+ 
    |    email|first_name| id|last_name|  date1|  date1| 
    +------------------+----------+---+---------+----------+----------+ 
    |[email protected]|  null| 3|  hh|2017-02-10|  null| 
    |[email protected]|  null| 3|  hh|  null|2017-02-12| 
    | [email protected]|  Steve| 1|  Kent|2017-02-08|  null| 
    +------------------+----------+---+---------+----------+----------+ 

使用したい場合は、それはleftantiかのかどうか、加入をfullnullのデフォルト値を見つける必要があります(私は以前のスレッドで議論したと思う)。

あなたはsubtractjoin気にもちょうどdrop列ことができます: "leftanti"

df = userLeft.drop("date1").subtract(userRight.drop("date1")) 
userLeft.join(df, df.columns).show() 

    +----------------+----------+---+---------+----------+ 
    |   email|first_name| id|last_name|  date1| 
    +----------------+----------+---+---------+----------+ 
    |[email protected]|  Steve| 1|  Kent|2017-02-08| 
    +----------------+----------+---+---------+----------+ 
+0

生産データです。私はNULLに触れてそれをデフォルト値に割り当てることはできません。 – orNehPraka

関連する問題