I need to convert this code into PySpark equivalent. I can not use pandas to create the dataframe.
This is how I create the dataframe using Pandas:
df['Name'] = np.random.choice(["Alex","James","Michael","Peter","Harry"], size=3)
df['ID'] = np.random.randint(1, 10, 3)
df['Fruit'] = np.random.choice(["Apple","Grapes","Orange","Pear","Kiwi"], size=3)
The dataframe should look like this in PySpark:
df
Name ID Fruit
Alex 3 Apple
James 6 Grapes
Harry 5 Pear
I have tried the following for 1 column:
sdf1 = spark.createDataFrame([(k,) for k in ['Alex','James', 'Harry']]).orderBy(rand()).limit(6).show()
CodePudding user response:
You can first create pandas dataframe then convert it into Pyspark dataframe. Or you can zip the 3 random numpy arrays and create spark dataframe like this:
import numpy as np
names = [str(x) for x in np.random.choice(["Alex", "James", "Michael", "Peter", "Harry"], size=3)]
ids = [int(x) for x in np.random.randint(1, 10, 3)]
fruits = [str(x) for x in np.random.choice(["Apple", "Grapes", "Orange", "Pear", "Kiwi"], size=3)]
df = spark.createDataFrame(list(zip(names, ids, fruits)), ["Name", "ID", "Fruit"])
df.show()
# ------- --- ------
#| Name| ID| Fruit|
# ------- --- ------
#| Peter| 8| Pear|
#|Michael| 7| Kiwi|
#| Harry| 4|Orange|
# ------- --- ------
CodePudding user response:
names = np.random.choice(["Alex","James","Michael","Peter","Harry"], size=3)
id = np.random.randint(1, 10, 3)
fruits = np.random.choice(["Apple","Grapes","Orange","Pear","Kiwi"], size=3)
columns = ['Name', 'ID', "Fruit"]
dataframe = spark.createDataFrame(zip(names, id, fruits), columns)
dataframe.show()