Importing and Exporting Data(CSV format) in Python- Pandas way

by | Oct 26, 2018 | Python | 0 comments

Here i am again with another chapter from  Data Analysis with Python   online course at cognitiveclass.ai. In this post we will deal with Panda for Importing and Exporting Data in Python. 

Hope you have gone through our previous post in (Python starters Archives | Python R)

Data used in the course- the Automobile Dataset is an online source, and it is in CSV (comma separated values) format. Let’s use this dataset as an example to practice data reading. data source: https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data data type: csv

How to Import Pandas

we import Pandas as follows:

import pandas as pd

What is DataFrame?

 

DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the most commonly used pandas object.

DataFrame accepts many different kinds of input:

  • Dict of 1D ndarrays, lists, dicts, or Series
  • 2-D numpy.ndarray
  • Structured or record ndarray
  • Series
  • Another DataFrame

Along with the data, you can optionally pass index (row labels) and columns (column labels) arguments.

 

 

The structure of a dataframe

pd.DataFrame([[1,2,3],[1,2,3]],columns=[‘A’,’B’,’C’])

AB
0123
1123

pd.DataFrame([[1,2,3],[1,2,3]],index=[‘Y’,’Z’],columns=[‘A’,’B’,’C’])

ABC
Y123
Z123

 

Read 10 Minutes to pandas tutorial.

To read any data using Python’s pandas package, there are two important factors to consider: format and file path.
Format is the way data is encoded. We can usually tell different encoding schemes by looking at the ending of the file name. Some common encodings are csv, json, xlsx, hdf and so forth.

The (file) path tells us where the data is stored. Usually it is stored either on the computer we are using, or online on the internet.

In pandas, the “read_csv()” method can read in files with columns separated by commas into a pandas DataFrame. Reading data in pandas can be done quickly in three lines.

First, import pandas.

Then define a variable with the file path.

And then use the read_csv method to import the data.

For example, if you would save the dataframe “df” as “automobile.csv” to your local machine, you may use the syntax below:

df.to_csv("automobile.csv")

However, “read_csv” assumes that the data contains a header. But if data has no column headers, so we need to specify “read_csv” to not assign headers by setting header to “none”.

After reading the dataset, it is a good idea to look at the dataframe to get a better intuition and to ensure that everything occurred the way you expected. we can just use dataframe.head() to show the first n rows of the data frame. You can also use Describe shows a quick statistic summary of your data dataframe.describe().

Similarly, dataframe.tail(n) shows the bottom n rows of data frame.

To export your pandas dataframe to a new CSV file. You can do this using the method, ”to_csv()” To do this, specify the file path (which includes the filename) that you want to write to.

For example, if you would like to save the dataframe “df” as “automobile.csv” to your own computer, you can use the syntax: df.to_csv (“automobile.csv”).

Pandas also supports importing and exporting of most data filetypes with different dataset formats. The code syntax for reading and saving other data formats is very similar to read or save csv file.

Each column shows a different method to read and save files into a different format.

Read/Save Other Data Formats

Same as read/save csv file, we use similar methods to read/save other dataset formats:

Data FormateReadSave
csvpd.read_csv()df.to_csv()
jsonpd.read_json()df.to_json()
excelpd.read_excel()df.to_excel()
hdfpd.read_hdf()df.to_hdf()
sqlpd.read_sql()df.to_sql()

Watch the Video

Join Today

Know more about latest trends, News in Field of Data Analytics.

We will offer Free SAS and Python Programme in Data Science & enhance your understanding of data analysis.

By rlochan2021

We offer Everything Free here and always will be, so joining is Risk Free and Always Cost Free.

Check Out These Related Posts

Functions in Python Programming for Data Science

I am really impressed with two online courses one is “Analytics in Python” at edX by ColombiaX and another is “Python for Data Science” at cognitiveclass.ai or (Previously Bigdatauniversity). So i am going to document what ever i will learn about functions in these two courses. First there are Built-in functions(Ready made for use), such […]

read more

String Functions in SAS and Python

I always believe SAS and Python can make a great team together for Data Scientists. So Why Now we study them together. In this Post we will discuss various String functions in SAS and Python. String Functions in Python 3 Hope you must have gone through my previous post “Python Programming- Strings explained“. len(): returns […]

read more

0 Comments

0 Comments

Submit a Comment

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