0

I am bit new on pyspark. I have a spark dataframe with about 5 columns and 5 records. I have list of 5 records. Now I want to add these 5 static records from the list to the existing dataframe using withColumn. I did that, but its not working. Any suggestions are greatly appreciated.

Below is my sample:

dq_results=[] 

for a in range(0,len(dq_results)):
    dataFile_df=dataFile_df.withColumn("dq_results",lit(dq_results[a]))
    print lit(dq_results[a])

thanks, Sreeram

pault
  • 41,343
  • 15
  • 107
  • 149
  • Thanks for your response..but my question is adding a column with custom list of values. The link provided, adds the new column from the existing dataframe. – sreeram ch Feb 01 '18 at 15:04

2 Answers2

1
dq_results=[] 

Create one data frame from list dq_results:

df_list=spark.createDataFrame(dq_results_list,schema=dq_results_col)

Add one column for df_list id (it will be row id)

df_list_id = df_list.withColumn("id", monotonically_increasing_id())

Add one column for dataFile_df id (it will be row id)

dataFile_df= df_list.withColumn("id", monotonically_increasing_id())

Now we can join the both dataframe df_list and dataFile_df.

dataFile_df.join(df_list,"id").show()

So dataFile_df is final data frame

pault
  • 41,343
  • 15
  • 107
  • 149
0

withColumn will add a new Column, but I guess you might want to append Rows instead. Try this:

df1 = spark.createDataFrame([(a, a*2, a+3, a+4, a+5) for a in range(5)], "A B C D E".split(' '))

new_data = [[100 + i*j for i in range(5)] for j in range(5)]

df1.unionAll(spark.createDataFrame(new_data)).show()

+---+---+---+---+---+
|  A|  B|  C|  D|  E|
+---+---+---+---+---+
|  0|  0|  3|  4|  5|
|  1|  2|  4|  5|  6|
|  2|  4|  5|  6|  7|
|  3|  6|  6|  7|  8|
|  4|  8|  7|  8|  9|
|100|100|100|100|100|
|100|101|102|103|104|
|100|102|104|106|108|
|100|103|106|109|112|
|100|104|108|112|116|
+---+---+---+---+---+
Glacier
  • 141
  • 5
  • Thanks for your response..but i need to add a new column with dynamic values from the list I have it to the dataframe.. – sreeram ch Feb 01 '18 at 13:42