Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • Gathering statistics with DBMS_STATS Oracle
  • Generate SSH without password authentication. Linux/Unix
  • TRUNCATE table and disabling referential constraints. Oracle
  • To seee semaphores and shared memory segments in Solaris Linux/Unix
  • Wait.sql Oracle
  • newupload.html PHP/MYSQL/Wordpress
  • My FTP Job Scheduling for www.pvmehta.com PHP/MYSQL/Wordpress
  • dbinv.sql Oracle
  • Jai Shree Ram Oracle
  • This is from Temi Oracle
  • Changing default shell Linux/Unix
  • Find_stale_dr.sql finding stale physical DR.. Oracle
  • Adding a new disk and mount it automatically. on VMWARE LINUX Linux/Unix
  • Establishing trusted relationship between dbmonitor( central monitoring) and monitoring targets. Linux/Unix
  • How to find who is using which Rollback segment and how many rows or blocks in that rollback segments, 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

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

Leave a Reply Cancel reply

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

Categories

  • Ansible (0)
  • AWS (2)
  • Azure (1)
  • Linux/Unix (149)
  • MYSQL (5)
  • Oracle (393)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (0)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (17)
  • rman-dataguard (26)
  • shell (149)
  • SQL scripts (342)
  • SQL Server (6)
  • Uncategorized (0)
  • Videos (0)

Recent Posts

  • Trace a SQL session from another session using ORADEBUG30-Sep-2025
  • SQL Server Vs Oracle Architecture difference25-Jul-2025
  • SQL Server: How to see historical transactions25-Jul-2025
  • SQL Server: How to see current transactions or requests25-Jul-2025
  • T-SQL Vs PL/SQL Syntax25-Jul-2025
  • Check SQL Server edition25-Jul-2025
  • Checking SQL Server Version25-Jul-2025
  • Oracle vs MYSQL Architecture differences (For DBAs)24-Jul-2025
  • V$INSTANCE of Oracle in MYSQL24-Jul-2025
  • Day to day MYSQL DBA operations (Compared with Oracle DBA)24-Jul-2025

Archives

  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • Move WordPress site from one hosting service to other. PHP/MYSQL/Wordpress
  • Find Plan Hash value fphv.sql Oracle
  • Kernel Parameters for Solaris Linux/Unix
  • Jai Shree Ram Oracle
  • shr1.sql for MTS or Shared server configuration Oracle
  • Configure ssh authentications for RAC Oracle
  • How to specify 2 arch location to avoid any kind of DB hanging. Oracle
  • Goog notes on X$ tables Oracle

Copyright © 2025 pvmehta.com.

Powered by PressBook News WordPress theme