将数据框的每一行转换为字符串
Convert each row of a dataframe to string
我正在尝试使用 pyspark 中的 hashlib.md5
为数据帧生成哈希码。它只接受一个字符串来生成哈希码。
我需要将数据帧的每一行转换为字符串。
我尝试了 concat_ws
函数来连接所有列并将其作为字符串但没有结果。
我的数据框有 Id, name, marks
列
我试过了:
str=df.select(concat_ws("id","name","marks"))
print(hashlib.md5(str.encode(encoding='utf_8', errors='strict')).hexdigest())
我收到这个错误:
AttributeError: 'DataFrame' object has no attribute 'encode'
你能试试吗
df.select("colname").rdd.map(lambda x: hashlib.md5(str(x).encode(encoding='utf_8', errors='strict')).hexdigest()).collect()
你应该会看到类似
的内容
['1dd55a7d40667d697743612f826b71e1', '64a537f89bd95f34374b619452b1a5ab']
在你的情况下,
df.select(expr("concat_ws(id,name,marks)").alias("mycolumn")).rdd.map(lambda x: hashlib.md5(str(x).encode(encoding='utf_8', errors='strict')).hexdigest()).collect()
我正在尝试使用 pyspark 中的 hashlib.md5
为数据帧生成哈希码。它只接受一个字符串来生成哈希码。
我需要将数据帧的每一行转换为字符串。
我尝试了 concat_ws
函数来连接所有列并将其作为字符串但没有结果。
我的数据框有 Id, name, marks
我试过了:
str=df.select(concat_ws("id","name","marks"))
print(hashlib.md5(str.encode(encoding='utf_8', errors='strict')).hexdigest())
我收到这个错误:
AttributeError: 'DataFrame' object has no attribute 'encode'
你能试试吗
df.select("colname").rdd.map(lambda x: hashlib.md5(str(x).encode(encoding='utf_8', errors='strict')).hexdigest()).collect()
你应该会看到类似
的内容['1dd55a7d40667d697743612f826b71e1', '64a537f89bd95f34374b619452b1a5ab']
在你的情况下,
df.select(expr("concat_ws(id,name,marks)").alias("mycolumn")).rdd.map(lambda x: hashlib.md5(str(x).encode(encoding='utf_8', errors='strict')).hexdigest()).collect()