0

I have a dataframe in python, df, that i want to pass to be able to use in % scala.

I have tried -

%python
pyDf.createOrReplaceTempView("testDF") // error message

2 Answers 2

1

it's not too difficult. I am sharing a sample code pls try. It's working in Pycharm or databricks.

from pyspark.sql import *

import pandas as pd

spark = SparkSession.builder.master("local").appName("testing").getOrCreate()

data = [['venu', 50], ['renu', 45], ['anu', 54],['bhanu',14]]

Create the pandas DataFrame

pdf= pd.DataFrame(data, columns = ['Name', 'Age'])

print(pdf)

Python Pands convert to Spark Dataframe.

sparkDF=spark.createDataFrame(pdf)

sparkDF.printSchema()

sparkDF.show()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

How can you access python object from scala ?
1

Just query it with spark.sql:

val scalaDf = spark.sql("select * from testDF")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.