Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • The most important Tuning Notes Oracle
  • good note for shared pool tunnig Oracle
  • tblwopk.sql tablewopk.sql Oracle
  • Python class import from different folders Python/PySpark
  • Sequence Resetting Oracle
  • V$INSTANCE of Oracle in MYSQL MYSQL
  • Jai Shree Ram Linux/Unix
  • 284785.1 How to check RAC Option is currently linked into the Oracle Binary Oracle
  • Rename Oracle Instance Name Oracle
  • Wait Based Tuning Step by step with SQL statement Oracle
  • Implementation of key based authentications Linux/Unix
  • Remove DOS CR/LFs (^M) Linux/Unix
  • Database link password in user_db_links Oracle
  • Drop database in Oracle 10g Oracle
  • How to Decide upto what level you can decrement your datafile size. ( Shrink Datafile) Oracle

Add new columns in dataframe

Posted on 30-Sep-202301-Oct-2023 By Admin No Comments on Add new columns in dataframe
from pyspark.sql.functions import col, lit

# File location and type
file_location = "/FileStore/tables/sales_data_part1.csv"
file_type = "csv"

# CSV options
infer_schema = "false"
first_row_is_header = "true"
delimiter = ","

# The applied options are for CSV files. For other file types, these will

# be ignored.
df = spark.read.format(file_type)
.option("inferSchema", infer_schema)
.option("header", first_row_is_header)
.option("sep", delimiter)
.load(file_location)

display(df)



# Adding new column with and with default values.

# Remember to import lit function from from

#pyspark.sql.functions
# Following code will add new column named

#COntinet with default value of North America

df2 = df.withColumn("Continent", lit("North America"))
df2.display()



# Adding new column based on existing row values
# Following code till add new column TotalPrice

# by multiplying Quantity and UnitPrice
df3 = df.withColumn("TotalPrice", col("Quantity")* col("UnitPrice"))
df3.display()



#Adding multiple columns
# Following code will add 2 columns,

# Total price = Quantity * UnitPrice and

# Region with default value as India
df4 = df.withColumn("TotalPrice", col("Quantity")* col("UnitPrice")).withColumn("Region", lit("India"))
df4.display()



#Adding column using SELECT
# Following code will create new DF with single column named Region and assigned

# default value of "India" to all its null values.
df5=df.select(lit("India").alias("Region"))
df5.display();



# To all all colums, you can use following code
df6=df.select(col("InvoiceNo"), col("StockCode"), col("Description"), col("Quantity"), col("InvoiceDate"), col("UnitPrice"), col("CustomerID"), col("Country"),lit("India").alias("Region"))
df6.display()

Python/PySpark

Post navigation

Previous Post: Getting started with notebook
Next Post: Load testing on Oracle 19C RAC with HammerDB

Related Posts

  • Getting started with notebook Python/PySpark
  • How to connect to Oracle Database with Wallet with Python. Oracle
  • Reading config file from other folder inside class Python/PySpark
  • Read CSV File using Python Python/PySpark
  • Read CSV file using PySpark Python/PySpark
  • Python class import from different folders Python/PySpark

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • AMC10 (0)
  • Ansible (0)
  • AWS (2)
  • Azure (1)
  • Django (0)
  • GIT (1)
  • Linux/Unix (150)
  • MYSQL (5)
  • Oracle (403)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (1)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (18)
  • rman-dataguard (26)
  • shell (151)
  • SQL scripts (349)
  • SQL Server (6)
  • Uncategorized (9)
  • Videos (0)

Recent Posts

  • Tutornet.US — Source of excellent learning14-Oct-2026
  • OCI Infrastructure Patching14-Oct-2026
  • Free API credits for AI models14-Oct-2026
  • AMC1020-Sep-2026
  • Key Management in Oracle: The Core Issue: Missing Master Key12-May-2026
  • SAT Mathematics 10 questions and answer at the end.30-Apr-2026
  • top 10 AI news today30-Apr-2026
  • runon_allpdbs_show_conname.sh23-Apr-2026
  • runon_allcdbs_find_pdbs.sh23-Apr-2026
  • Running PDB on single node in RAC09-Apr-2026

Archives

  • 2026
  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • Optimizer_Index init.ora parameter explaination. Oracle
  • PLSQL Table Syntax 1 Oracle
  • ext#.sql Oracle
  • how to find OS block size Oracle
  • How To Limit The Access To The Database So That Only One User Per Schema Are Connected (One Concurrent User Per Schema) Oracle
  • My Minimum Tuning Programs Oracle
  • RMAN : Consistent Backup, Restore and Recovery using RMAN Oracle
  • Giving Grant on v$DATABASE Oracle

Copyright © 2026 pvmehta.com.

Powered by PressBook News WordPress theme