Pyspark Learning Series (ii) data processing by reading CSV files for RDD or dataframe

Source: Internet
Author: User
Tags pyspark databricks

First, local CSV file read:

The easiest way:

Import pandas as PD
lines = pd.read_csv (file)
lines_df = Sqlcontest.createdataframe (lines)

Or use spark to read directly as Rdd and then in the conversion

lines = sc.textfile (' file ')
If your CSV file has a title, you need to remove the first line
Header = Lines.first () #第一行 
lines = lines.filter (lambda row:row!= header) #删除第一行

At this time lines for RDD. If you need to convert to Dataframe:

schema = Structtype ([Structfield (' HWMC ', StringType (), True), Structfield (' Code ', StringType (), true)]
LINES_DF = Sqlcontest.createdataframe (Lines,schema)

Second, HDFs on the CSV file read:

1, using the form of first reading for RDD conversion

2, the use of SqlContext.read.format (), this has a prerequisite to do well in advance rely on com.databricks.spark.csv

SqlContext = SqlContext (SC)
sqlContext.read.format (' com.databricks.spark.csv '). Options (header= ' true ', Inferschema= ' true '). Load (' file ')




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.