18

次のスキーマを持つ DataFrame があるxとします。

xSchema = StructType([ \
    StructField("a", DoubleType(), True), \
    StructField("b", DoubleType(), True), \
    StructField("c", DoubleType(), True)])

次に、DataFrame を取得します。

DataFrame[a :double, b:double, c:double]

整数の派生列が必要です。ブール列を作成できます:

x = x.withColumn('y', (x.a-x.b)/x.c > 1)

私の新しいスキーマは次のとおりです。

DataFrame[a :double, b:double, c:double, y: boolean]

yただし、列に False の場合は 0、True の場合は 1 を含めたいと思います。

cast関数は列ではなく でDataFrameのみ操作でき、関数withColumnは でのみ操作できDataFrameます。新しい列を追加して同時に整数にキャストするにはどうすればよいですか?

4

1 に答える 1

29

使用する式は列に評価されるため、次のように直接キャストできます。

x.withColumn('y', ((x.a-x.b) / x.c > 1).cast('integer')) # Or IntegerType()
于 2015-10-26T20:20:46.133 に答える