2017-12-27 15 views
1

を使用してデータフレームを作成する私はPySparkのv1.6.1を使用していると私は別のものを使用してデータフレームを作成したい:Pyspark:どのように他のデータフレーム

  • 異なるにおける3つの値の構造体を持つフィールドを変換します列
  • 変更に
  • 列名とタイプ
の残りの部分をそのタイムスタンプを使用して複数の列を作成し、文字列からdatatime
  • にタイムスタンプを変換します

    現在、.map(func)を使用して、そのファンクションを使用してRDDを作成しています(元のタイプの1つの行から変換し、新しいタイプの行を返します)。しかし、これはRDDを作成していると私はそれを失うことはありません。

    これを行うにはより良い方法がありますか?

    ありがとうございました!

  • +0

    、あなたは簡単にDFにそれを変換することができます。 –

    +0

    私はRDDを作成したくないですが、RDDを使用しないようにしたいのは、Python用のパフォーマンスボトルネックです.DF変換をしたいだけです。 – frm

    +1

    私たちが手助けできるようにいくつかのコードを入力してください。その間、 'pyspark.sql.functions.udf'と火花データフレームの' withColumn() 'メソッドを調べてください。 – pault

    答えて

    1

    希望すると便利です。

    from pyspark.sql.functions import unix_timestamp, col, to_date, struct 
    
    #### 
    #sample data 
    #### 
    df = sc.parallelize([[25, 'Prem', 'M', '12-21-2006 11:00:05','abc', '1'], 
             [20, 'Kate', 'F', '05-30-2007 10:05:00', 'asdf', '2'], 
             [40, 'Cheng', 'M', '12-30-2017 01:00:01', 'qwerty', '3']]).\ 
        toDF(["age","name","sex","datetime_in_strFormat","initial_col_name","col_in_strFormat"]) 
    
    #create 'struct' type column by combining first 3 columns of sample data - (this is built to answer query #1) 
    df = df.withColumn("struct_col", struct('age', 'name', 'sex')).\ 
        drop('age', 'name', 'sex') 
    df.show() 
    df.printSchema() 
    
    #### 
    #query 1 
    #### 
    #Convert a field that has a struct of three values (i.e. 'struct_col') in different columns (i.e. 'name', 'age' & 'sex') 
    df = df.withColumn('name', col('struct_col.name')).\ 
        withColumn('age', col('struct_col.age')).\ 
        withColumn('sex', col('struct_col.sex')).\ 
        drop('struct_col') 
    df.show() 
    df.printSchema() 
    
    #### 
    #query 2 
    #### 
    #Convert the timestamp from string (i.e. 'datetime_in_strFormat') to datetime (i.e. 'datetime_in_tsFormat') 
    df = df.withColumn('datetime_in_tsFormat', 
            unix_timestamp(col('datetime_in_strFormat'), 'MM-dd-yyyy hh:mm:ss').cast("timestamp")) 
    df.show() 
    df.printSchema() 
    
    #### 
    #query 3 
    #### 
    #create more columns using above timestamp (e.g. fetch date value from timestamp column) 
    df = df.withColumn('datetime_in_dateFormat', to_date(col('datetime_in_tsFormat'))) 
    df.show() 
    
    #### 
    #query 4.a 
    #### 
    #Change column name (e.g. 'initial_col_name' is renamed to 'new_col_name) 
    df = df.withColumnRenamed('initial_col_name', 'new_col_name') 
    df.show() 
    
    #### 
    #query 4.b 
    #### 
    #Change column type (e.g. string type in 'col_in_strFormat' is coverted to double type in 'col_in_doubleFormat') 
    df = df.withColumn("col_in_doubleFormat", col('col_in_strFormat').cast("double")) 
    df.show() 
    df.printSchema() 
    

    サンプルデータ:

    +---------------------+----------------+----------------+------------+ 
    |datetime_in_strFormat|initial_col_name|col_in_strFormat| struct_col| 
    +---------------------+----------------+----------------+------------+ 
    | 12-21-2006 11:00:05|    abc|    1| [25,Prem,M]| 
    | 05-30-2007 10:05:00|   asdf|    2| [20,Kate,F]| 
    | 12-30-2017 01:00:01|   qwerty|    3|[40,Cheng,M]| 
    +---------------------+----------------+----------------+------------+ 
    root 
    |-- datetime_in_strFormat: string (nullable = true) 
    |-- initial_col_name: string (nullable = true) 
    |-- col_in_strFormat: string (nullable = true) 
    |-- struct_col: struct (nullable = false) 
    | |-- age: long (nullable = true) 
    | |-- name: string (nullable = true) 
    | |-- sex: string (nullable = true) 
    

    最終的な出力データ:すでにRDDを作成することができるならば

    +---------------------+------------+----------------+-----+---+---+--------------------+----------------------+-------------------+ 
    |datetime_in_strFormat|new_col_name|col_in_strFormat| name|age|sex|datetime_in_tsFormat|datetime_in_dateFormat|col_in_doubleFormat| 
    +---------------------+------------+----------------+-----+---+---+--------------------+----------------------+-------------------+ 
    | 12-21-2006 11:00:05|   abc|    1| Prem| 25| M| 2006-12-21 11:00:05|   2006-12-21|    1.0| 
    | 05-30-2007 10:05:00|  asdf|    2| Kate| 20| F| 2007-05-30 10:05:00|   2007-05-30|    2.0| 
    | 12-30-2017 01:00:01|  qwerty|    3|Cheng| 40| M| 2017-12-30 01:00:01|   2017-12-30|    3.0| 
    +---------------------+------------+----------------+-----+---+---+--------------------+----------------------+-------------------+ 
    
    root 
    |-- datetime_in_strFormat: string (nullable = true) 
    |-- new_col_name: string (nullable = true) 
    |-- col_in_strFormat: string (nullable = true) 
    |-- name: string (nullable = true) 
    |-- age: long (nullable = true) 
    |-- sex: string (nullable = true) 
    |-- datetime_in_tsFormat: timestamp (nullable = true) 
    |-- datetime_in_dateFormat: date (nullable = true) 
    |-- col_in_doubleFormat: double (nullable = true) 
    
    +0

    こんにちはプレム!、そのコードは私が探していたものでした!ありがとう! – frm

    +0

    助けてくれてうれしい! – Prem

    関連する問題