Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • move_arch_files.ksh Linux/Unix
  • 751131.1 New Article Error 2819 While Requesting a Systemstate Dump Oracle
  • Database link password in user_db_links Oracle
  • ipcs -l Linux/Unix
  • Sending email with file attachment. Linux/Unix
  • ORA-00064: object is too large to allocate on this O/S during startup Oracle
  • Jai Shree Ram Oracle
  • Search and replace pattern Linux/Unix
  • shutdown linux Linux/Unix
  • Jai Shree Ram Oracle
  • Oracle 10g Wait Model Oracle
  • For Search and replace unix command. Linux/Unix
  • Giving Grant on v$DATABASE Oracle
  • OEM-Commnds Oracle
  • Jai Shree Ram 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

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

Leave a Reply Cancel reply

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

Categories

  • AWS (2)
  • Azure (1)
  • Linux/Unix (149)
  • Oracle (388)
  • PHP/MYSQL/Wordpress (10)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (17)
  • rman-dataguard (26)
  • shell (149)
  • SQL scripts (337)
  • Uncategorized (0)
  • Videos (0)

Recent Posts

  • findinfo.sql (SQL for getting CPU and Active session info)27-May-2025
  • SQL Tracker by SID sqltrackerbysid.sql22-Apr-2025
  • How to connect to Oracle Database with Wallet with Python.21-Mar-2025
  • JSON/XML Types in Oracle18-Mar-2025
  • CPU Core related projections12-Mar-2025
  • Exadata Basics10-Dec-2024
  • Reading config file from other folder inside class24-Sep-2024
  • Python class import from different folders22-Sep-2024
  • Transfer SQL Profiles from One database to other database.05-Sep-2024
  • Load testing on Oracle 19C RAC with HammerDB18-Jan-2024

Archives

  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • DETERMINING WHICH INSTANCE OWNS WHICH SHARED MEMORY & SEMAPHORE SEGMENTS Oracle
  • Some useful Unix Commands Linux/Unix
  • USE_NL and INDEX hints example Oracle
  • sql_plan9i.sql Oracle
  • remove archfiles only when it is applied to DR rm_archfiles.sh Linux/Unix
  • get_vmstat_linux Oracle
  • Goldengate document from Porus Oracle
  • chk_space_SID.ksh Linux/Unix

Copyright © 2025 pvmehta.com.

Powered by PressBook News WordPress theme