Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

PySpark process Multi char Delimiter Dataset
Latest   Machine Learning

PySpark process Multi char Delimiter Dataset

Last Updated on July 19, 2023 by Editorial Team

Author(s): Vivek Chaudhary

Originally published on Towards AI.

Programming

The objective of this article is to process multiple delimited files using Apache spark with Python Programming language. This is a real-time scenario where an application can share multiple delimited file,s and the Dev Team has to process the same. We will learn how we can handle the challenge.

The input Data set is as below:

Name@@#Age <--Header
vivek, chaudhary@@#30 <--row1
john, morgan@@#28 <--row2

Approach1: Let’s try to read the file using read.csv() and see the output:

from pyspark.sql import SparkSessionfrom pyspark.sql import SparkSession
spark= SparkSession.builder.appName(β€˜multiple_delimiter’).getOrCreate()
test_df=spark.read.csv(β€˜D:\python_coding\pyspark_tutorial\multiple_delimiter.csv’)
test_df.show()
Output

#Note: Output is not the desired one and so the processing will not yield the desired results

Approach2: Next, read the file using read.csv() with option() parameter and pass the delimiter as an argument having the value β€˜@@#’ and see the output:

test_df=spark.read.option(β€˜delimiter’,’@@#’).csv(β€˜D:\python_coding\pyspark_tutorial\multiple_delimiter.csv’)test_df.show(truncate=0)
error

#Note: spark throws error when we try to pass delimiter of more than one character.

Approach3: Next way is to use read.text() method of spark.

mult_df=spark.read.text(β€˜D:\python_coding\pyspark_tutorial\multiple_delimiter.csv’)
mult_df.show(truncate=0)
spark.read.text

#Note: spark.read.text returns a DataFrame.

Each line in a text file represents a record in DataFrame with just one column β€œvalue”. To convert into multiple columns, we will use map transformation and split method to transform and split the column values.

#first() returns the first record of dataset
header=mult_df.first()[0]
print(header)
Output:
Name@@#Age
#split('delimiter') the string on basis of the delimiter
#define the schema of the Dataframe to be created
schema=header.split(β€˜@@#’)
print(schema)
Output:
['Name', 'Age']

The next step is to split the row and create separate columns:

#filter operation is removing the header
#map operation is splitting each record as per delimiter
#.rdd converts DF to rdd and toDF converts the rdd back to DF
mult_df.filter(mult_df[β€˜value’]!=header).rdd.map(lambda x:x[0].split(β€˜@@#’)).toDF(schema).show()
Final Output

Hurray!! We are able to split the data on the basis of multiple delimiter β€˜@@#’.

Summary:

Β· Read Multiple Delimited Dataset using spark.read.text() method

Β· use of map(), filter() transformations

Thanks to all for reading my blog, and If you like my content and explanation, please follow me on medium and share your feedback, which will always help all of us to enhance our knowledge.

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.

Published via Towards AI

Feedback ↓