Coder Social home page Coder Social logo

databricks's Introduction

DataBricks

Creating Data Frame

Method-1

data=[(1,'Aman'),(2,'Akash')]
hedaer_schema = ['id','Name']
df = spark.createDataFrame(data,hedaer_schema)
df.show()

Method-2

data=[{'id':1,'Name':'Aman'},{'id':2,'Name':'Akash'}]
df = spark.createDataFrame(data)
df.show()

Method-3

from pyspark.sql.types import StructType,StructField,StringType,IntegerType
data=[{'id':1,'Name':'Aman'},{'id':2,'Name':'Akash'}]
hedaer_schema = StructType([StructField(name='id',dataType=IntegerType()),
  StructField(name='Name',dataType=StringType())
])
df = spark.createDataFrame(data)
df.show()

Reading Csv

Method-1

path = 'dbfs:/FileStore/tables/effects_of_covid_19_on_trade_at_15_december_2021_provisional.csv';
df = spark.read.csv(path,header=True,inferSchema=True)
display(df)

Method-2

path = 'dbfs:/FileStore/tables/effects_of_covid_19_on_trade_at_15_december_2021_provisional.csv';
df = spark.read.format('csv').option(key='header',value=True).load(path)
display(df)

Reading Multiple Csv

#Schema should be same
path1 = 'dbfs:/FileStore/tables/effects_of_covid_19_on_trade_at_15_december_2021_provisional.csv';
path2 = 'dbfs:/FileStore/tables/effects_of_covid_19_on_trade_at_15_december_2021_provisional.csv';
df = spark.read.csv(path=[path1,path2],header=True,inferSchema=True)
display(df)

#All file schema should be same in this folder
folder_path = 'dbfs:/FileStore/tables/';
df = spark.read.csv(path=folder_path,header=True,inferSchema=True)
display(df)

Note : We can also pass schema if we want to read only some columns

Write dataframe into Csv

#Creating Data frame
data=[(1,'Aman'),(2,'Akash')]
hedaer_schema = ['id','Name']
path='dbfs:/FileStore/tables/maydata'
df = spark.createDataFrame(data,hedaer_schema)
df.write.option("header",True).csv(path,header=True,mode='overWrite')
# For reading data you can just provide the folder path

NOTE : Read ,write operation for json

show()

path = 'dbfs:/FileStore/tables/effects_of_covid_19_on_trade_at_15_december_2021_provisional.csv';
df=spark.read.csv(path=path,header=True,inferSchema=True);
df.show(n=4,truncate=False,vertical=True) #n number of colums,vertical direction of data to show

withColumn()

from pyspark.sql.functions import col
path = 'dbfs:/FileStore/tables/effects_of_covid_19_on_trade_at_15_december_2021_provisional.csv';
df=spark.read.csv(path=path,header=True,inferSchema=True);
df=df.withColumn('Updated Value',col=col('Value')/100) #Here we can also cast the value .cast('Integer')
df.show(n=2,vertical=True)

withColumnRenamed()

path = 'dbfs:/FileStore/tables/effects_of_covid_19_on_trade_at_15_december_2021_provisional.csv';
df=spark.read.csv(path=path,header=True,inferSchema=True);
df=df.withColumnRenamed('Country','RenamedCountry')
df.show()

StructType() StructField()

NOTE : You can also pass ArrayType(IntegerType)

Method-1

from pyspark.sql.types import StructType,StructField,StringType,IntegerType
data=[{'id':1,'Name':'Aman'},{'id':2,'Name':'Akash'}]
hedaer_schema = StructType([StructField(name='id',dataType=IntegerType()),
  StructField(name='Name',dataType=StringType())
])
df = spark.createDataFrame(data)
df.show()

Method-2

from pyspark.sql.types import StructType,StructField,StringType,IntegerType
path = 'dbfs:/FileStore/tables/effects_of_covid_19_on_trade_at_15_december_2021_provisional.csv';
hedaer_schema = StructType([\
    StructField(name='RenamedCountry',dataType=StringType()),\
      StructField(name='Value',dataType=IntegerType())
])
df=spark.read.csv(path=path,header=True,schema=hedaer_schema);
df.display()

databricks's People

Contributors

aman-abesec avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.