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

DataFrame

Gk
New Contributor III

How can we create empty dataframe in databricks and how many ways we can create dataframe?

2 REPLIES 2

Anonymous
Not applicable

@Govardhana Reddy​ :

Method 1:

from pyspark.sql import SparkSession
 
spark = SparkSession.builder.appName("MyApp").getOrCreate()
 
# Create an empty DataFrame with a specified schema
empty_df = spark.createDataFrame([], schema=["column1", "column2", "column3"])
empty_df.show()

Method 2: From dictionary

data = [
  {"name": "Alice", "age": 25},
  {"name": "Bob", "age": 30},
  {"name": "Charlie", "age": 35}
]
df = spark.createDataFrame(data)
df.show()

Method 3: From list of tuples

data = [("Alice", 25), ("Bob", 30), ("Charlie", 35)]
df = spark.createDataFrame(data, schema=["name", "age"])
df.show()

Method 4: From Pandas dataframe

import pandas as pd
 
pdf = pd.DataFrame({
  "name": ["Alice", "Bob", "Charlie"],
  "age": [25, 30, 35]
})
df = spark.createDataFrame(pdf)
df.show()

Method 5: from cvs file

df = spark.read.csv("path/to/file.csv", header=True, inferSchema=True)
df.show()

Method 6: From parquet file

df = spark.read.parquet("path/to/file.parquet")
df.show()

Vartika
Moderator
Moderator

Hi @Govardhana Reddy​ 

Hope everything is going great.

Does @Suteja Kanuri​'s answer help? If yes, would you be happy to mark an answer as best so that other members can find the solution more quickly? If not, please tell us so we can help you. 

Cheers!

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.