cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

Am trying to use SQL, but createOrReplaceTempView("myDataView")​ fails

MikeBrewer
New Contributor II

Am trying to use SQL, but createOrReplaceTempView("myDataView") fails.

I can create and display a DataFrame fine...

import pandas as pd
df = pd.DataFrame(['$3,000,000.00','$3,000.00', '$200.5', '$5.5'], columns = ['Amount'])
df

I add another cell, but it fails...

df.createOrReplaceTempView("myDataView")

I get this error..

'DataFrame' object has no attribute 'createOrReplaceTempView'

I see this example out there on the net allot, but don't understand why it fails for me. I am using

Community edition. 6.5 (includes Apache Spark 2.4.5, Scala 2.11)

3 REPLIES 3

-werners-
Esteemed Contributor III

I never worked with pandas on spark, but a pandas dataframe is not the same as a spark dataframe.

You need to convert it to a spark dataframe first with Koalas f.e.

https://koalas.readthedocs.io/en/latest/user_guide/pandas_pyspark.html#pyspark

acorson
New Contributor II

You need to convert the pandas DF to an spark DF. Enabling Apache Arrow will make this process faster.

From https://docs.databricks.com/spark/latest/spark-sql/spark-pandas.html:

import numpy as np import pandas as pd

Enable Arrow-based columnar data transfers

park.conf.set("spark.sql.execution.arrow.enabled", "true")

Generate a pandas DataFrame

df = pd.DataFrame(np.random.rand(100, 3))

Create a Spark DataFrame from a pandas DataFrame using Arrow

f = spark.createDataFrame(pdf)

Convert the Spark DataFrame back to a pandas DataFrame using Arrow

esult_pdf = df.select("*").toPandas()

sachinthana
New Contributor II

This is worked for me. Thank you @acorson​ 

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.