Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Data Analysis

You're reading from   Python Data Analysis Perform data collection, data processing, wrangling, visualization, and model building using Python

Arrow left icon
Product type Paperback
Published in Feb 2021
Publisher Packt
ISBN-13 9781789955248
Length 478 pages
Edition 3rd Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Ivan Idris Ivan Idris
Author Profile Icon Ivan Idris
Ivan Idris
Avinash Navlani Avinash Navlani
Author Profile Icon Avinash Navlani
Avinash Navlani
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Section 1: Foundation for Data Analysis
2. Getting Started with Python Libraries FREE CHAPTER 3. NumPy and pandas 4. Statistics 5. Linear Algebra 6. Section 2: Exploratory Data Analysis and Data Cleaning
7. Data Visualization 8. Retrieving, Processing, and Storing Data 9. Cleaning Messy Data 10. Signal Processing and Time Series 11. Section 3: Deep Dive into Machine Learning
12. Supervised Learning - Regression Analysis 13. Supervised Learning - Classification Techniques 14. Unsupervised Learning - PCA and Clustering 15. Section 4: NLP, Image Analytics, and Parallel Computing
16. Analyzing Textual Data 17. Analyzing Image Data 18. Parallel Computing Using Dask 19. Other Books You May Enjoy

Reading and writing CSV files with pandas

The pandas library provides a variety of file reading and writing options. In this section, we will learn about reading and writing CSV files. In order to read a CSV file, we will use the read_csv() method. Let's see an example:

# import pandas
import pandas as pd

# Read CSV file
sample_df=pd.read_csv('demo.csv', sep=',' , header=None)

# display initial 5 records
sample_df.head()

This results in the following output:

We can now save the dataframe as a CSV file using the following code:

# Save DataFrame to CSV file
sample_df.to_csv('demo_sample_df.csv')

In the preceding sample code, we have read and saved the CSV file using the read_csv() and to_csv(0) methods of the pandas module.

The read_csv() method has the following important arguments:

  • filepath_or_buffer: Provides a file path or URL as a string to read a file.
  • sep: Provides a separator in the string, for example, comma as ',' and semicolon as &apos...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image