简体   繁体   English

如何从 Pyspark 数据帧的每一行中提取我不知道的值

[英]How to extract a value that i don´t know from every row of a Pyspark dataframe

I have a dataframe like this:我有一个这样的数据框:

item_A  item_B item_C
  x       z      y
  z       x      y
  y       x      z
  z       y      x 

where all values are a string and I only know the value of x and y but i need to get the value of z .其中所有值都是字符串,我只知道xy的值,但我需要获取z的值。 The problem is that z is not always in the same column.问题是z并不总是在同一列中。 I want to add a column only with the value of z .我只想添加一个值为z的列。 I tried concatenating the columns and extract the others strings that I know but I don't know how to keep z (with a main_pattern = r'x|y' ?)我尝试连接列并提取我知道但我不知道如何保留z的其他字符串(使用main_pattern = r'x|y' ?)

Here is what I tried but isn't working这是我尝试过但不起作用的方法

pattern_full = r'(('+ main_pattern+'),)'
df = df.withColumn("vale_z", regexp_extract("columns_concatenated", pattern_full, 1)

We can create an array with all 3 columns and use array_except() to remove the known values which leaves us with the "Z" .我们可以创建一个包含所有 3 列的数组,并使用array_except()删除已知值,这给我们留下了"Z"

data_sdf = spark.sparkContext.parallelize(data_ls).toDF(['col1', 'col2', 'col3'])

# +----+----+----+
# |col1|col2|col3|
# +----+----+----+
# |   x|   z|   y|
# |   z|   y|   x|
# |   x|   y|   y|
# |   x|   z|   z|
# +----+----+----+

data_sdf. \
    withColumn('col_array', func.array(func.col('col1'), func.col('col2'), func.col('col3'))). \
    withColumn('z_val_arr', func.array_except('col_array', func.array(func.lit('x'), func.lit('y')))). \
    withColumn('z_val', func.col('z_val_arr')[0]). \
    show(truncate=False)

# +----+----+----+---------+---------+-----+
# |col1|col2|col3|col_array|z_val_arr|z_val|
# +----+----+----+---------+---------+-----+
# |x   |z   |y   |[x, z, y]|[z]      |z    |
# |z   |y   |x   |[z, y, x]|[z]      |z    |
# |x   |y   |y   |[x, y, y]|[]       |null |
# |x   |z   |z   |[x, z, z]|[z]      |z    |
# +----+----+----+---------+---------+-----+
d1 = [['x', 'z', 'y'], ['z', 'x', 'y'], ['y', 'x', 'z'], ['z', 'y', 'x'], ['u', 'v', 'w']]
df1 = spark.createDataFrame(d1, ['item_A', 'item_B', 'item_C'])

df1.withColumn('columns_concatenated', concat(*df1.columns))\
    .withColumn('find_z', regexp_extract(col('columns_concatenated'), '(z)', 1))\
    .show(10, False)
+------+------+------+--------------------+------+
|item_A|item_B|item_C|columns_concatenated|find_z|
+------+------+------+--------------------+------+
|x     |z     |y     |xzy                 |z     |
|z     |x     |y     |zxy                 |z     |
|y     |x     |z     |yxz                 |z     |
|z     |y     |x     |zyx                 |z     |
|u     |v     |w     |uvw                 |      |
+------+------+------+--------------------+------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM