cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

transform a dataframe column as concatenated string

geertvanhove
New Contributor III

Hello,
I have a single column dataframe and I want to transform the content into a string

EG df=

abc

def

xyz

To

abc, def, xyz

 

Thanks

3 REPLIES 3

Thanks, but can you give me an example for this because following still gives me an error: 

result = ', '.join(df.select(col("meterId")).tolist())

geertvanhove
New Contributor III

I get following error:

'DataFrame' object has no attribute 'tolist'

geertvanhove
New Contributor III

sure:

 

%python
from pyspark.sql.functions import from_json, col, concat_ws
from pyspark.sql.types import *

schema = StructType([StructField('meterDateTime', StringType(), True), StructField('meterId', LongType(), True), StructField('meteringState', StringType(), True), StructField('value', DoubleType(), True), StructField('versionTimestamp', StringType(), True), StructField('file_name', StringType(), False), StructField('file_modification_time', TimestampType(), False)])

df = ( spark
        .read
        .format("json")
        .schema(schema)
        .load(f'{path_sep}/*/*/*/*.json')
        .select("meterId")
  )

result = ', '.join(df.tolist())
print(result)

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group