是否有任何Pyspark UDF函数或内置功能可用于在数据框中添加新列并根据行值进行行级操作?

发布于 2025-01-26 02:07:42 字数 666 浏览 2 评论 0原文

我有一个类似的数据框:

    | col1 | col2 |
    --------------
    | a    | 1    |
    | a    | 2    |
    | b    | 3    |
    | c    | 4    |
    | a    | 5    |

现在,我需要创建新的列“ Col3”,并且必须根据Col1值将新值放在Col3中。最终的数据帧看起来像这样。

就像,如果Col1具有“ A”的值,则Col3应该在其中具有“苹果”。 如果Col1具有“ B”的价值,则Col3应该在其中有“香蕉”。 如果Col1具有“ C”的价值,则Col3应该在其中具有“蛋ust”。

注意:COL2是正常的列,请不要考虑。

    | col1 | col2 | col3    |
    ------------------------
    | a    | 1    |apple    |
    | a    | 2    |apple    |
    | b    | 3    |banana   |
    | c    | 4    |custard  |
    | a    | 5    |apple    |

我可以获得的任何pyspark UDF或内置功能吗?

提前致谢!!!

I have a dataframe like this:

    | col1 | col2 |
    --------------
    | a    | 1    |
    | a    | 2    |
    | b    | 3    |
    | c    | 4    |
    | a    | 5    |

Now, I need to create new column 'col3' and i have to put new values in col3 based on col1 value. The resultant dataframe would look like this.

Like, if col1 has the value 'a', then col3 should have "apple" in it.
if col1 has the value 'b', then col3 should have "banana" in it.
if col1 has the value 'c', then col3 should have "custard" in it.

Note: col2 is normal column, Please don't consider.

    | col1 | col2 | col3    |
    ------------------------
    | a    | 1    |apple    |
    | a    | 2    |apple    |
    | b    | 3    |banana   |
    | c    | 4    |custard  |
    | a    | 5    |apple    |

Any Pyspark UDF or Inbuilt function i can get?

Thanks in Advance!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窝囊感情。 2025-02-02 02:07:42
from pyspark.sql import SparkSession
spark=SparkSession.builder.appName("test").getOrCreate()
data=[('a',1),('a',2),('b',3),('c',4),('a',5)]
headers = ("col1", "col2")
df=spark.createDataFrame(data,headers)
df.show(truncate=0)

#defining UDF
keywords = {
'a': 'apple',
'b': 'banana',
'c': 'custard'
}

def words(col1Value):
    return keywords.get(col1Value, "Not Found ! ")

from pyspark.sql.functions import udf
from pyspark.sql.types import *
#Converting function to UDF 
convertUDF = udf(lambda z: words(z),StringType())

import pyspark.sql.functions as F
finaloutput=df.withColumn("col3", convertUDF(df['col1']))
finaloutput.show(truncate=0)

DataFram记录:

+----+----+
|col1|col2|
+----+----+
|a   |1   |
|a   |2   |
|b   |3   |
|c   |4   |
|a   |5   |
+----+----+

输出:

+----+----+-------+
|col1|col2|col3   |
+----+----+-------+
|a   |1   |apple  |
|a   |2   |apple  |
|b   |3   |banana |
|c   |4   |custard|
|a   |5   |apple  |
+----+----+-------+
from pyspark.sql import SparkSession
spark=SparkSession.builder.appName("test").getOrCreate()
data=[('a',1),('a',2),('b',3),('c',4),('a',5)]
headers = ("col1", "col2")
df=spark.createDataFrame(data,headers)
df.show(truncate=0)

#defining UDF
keywords = {
'a': 'apple',
'b': 'banana',
'c': 'custard'
}

def words(col1Value):
    return keywords.get(col1Value, "Not Found ! ")

from pyspark.sql.functions import udf
from pyspark.sql.types import *
#Converting function to UDF 
convertUDF = udf(lambda z: words(z),StringType())

import pyspark.sql.functions as F
finaloutput=df.withColumn("col3", convertUDF(df['col1']))
finaloutput.show(truncate=0)

datafram records:

+----+----+
|col1|col2|
+----+----+
|a   |1   |
|a   |2   |
|b   |3   |
|c   |4   |
|a   |5   |
+----+----+

output:

+----+----+-------+
|col1|col2|col3   |
+----+----+-------+
|a   |1   |apple  |
|a   |2   |apple  |
|b   |3   |banana |
|c   |4   |custard|
|a   |5   |apple  |
+----+----+-------+
彼岸花似海 2025-02-02 02:07:42

我得到了这个功能的答案。这可能对某人有帮助。

我使用了此功能:

    fruits = {
'a': 'apple',
'b': 'banana',
'c': 'custard'
}

    def X(col1Value):
        return fruits.get(col1Value, "Not Found ! ")



    df['col3']= X(col1Value)

修改后的变量!!!

I got an Answer by this function.. This could be helpful for someone.

I have used this function:

    fruits = {
'a': 'apple',
'b': 'banana',
'c': 'custard'
}

    def X(col1Value):
        return fruits.get(col1Value, "Not Found ! ")



    df['col3']= X(col1Value)

Modified variables!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文