10 Minutes to Dataframe in Pandas
By: Karan Shah
Tags: Data Analysis, Pandas, Python, Data Science, Data Visualization

10 Minutes to Dataframe in Pandas

Learn and Become a Master of one of the most used Python tools for Data Analysis.

Introduction:-

Pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.

The Data Structures provided by Pandas are of two distinct types

Pandas DataFrames & Pandas Series

We’ll look at Pandas Dataframes in this post.

What is Data frames ?

A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns.

A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. Pandas DataFrame consists of three principal components, the data, rows, and columns.

Features of DataFrame

Potentially columns are of different types.

Size — Mutable.

Labeled axes (rows and columns).

Can Perform Arithmetic operations on rows and columns.

Diffrence between Series and DataFrame

Sources

You can think of it as an SQL table or a spreadsheet data representation.

How to Create a Dataframe ?

A pandas DataFrame can be created using the following constructor −

pandas.DataFrame( data, index, columns, dtype, copy)

The parameters of the constructor are as follows −

Data : data takes various forms like ndarray, series, map, lists, dict, constants and also another DataFrame.

data takes various forms like ndarray, series, map, lists, dict, constants and also another DataFrame. index : For the row labels, the Index to be used for the resulting frame is Optional Default np.arange(n) if no index is passed.

For the row labels, the Index to be used for the resulting frame is Optional Default np.arange(n) if no index is passed. columns : For column labels, the optional default syntax is — np.arange(n). This is only true if no index is passed.

For column labels, the optional default syntax is — np.arange(n). This is only true if no index is passed. dtype : Data type of each column.

Data type of each column. copy : This command (or whatever it is) is used for copying of data, if the default is False.

Reading the data and first insights

The first thing we should do, once we have downloaded or collected some data is to read such data into a pandas DataFrame. This is one of the main Pandas objects, along with the Series, and like I mentioned before, it resembles a table with columns and rows. Before, as always, we should import the library.

Pandas also has functions for reading from Excel sheets, HTML documents, or SQL databases (although there are other tools that are better for reading from databases)

We can check out the first n rows of our dataframe using the head method. There is also a tail method to look at the last n. By default if no n is given to these methods they return the first 5 or last 5 instances.

Using the head method without a parameter returns the following block:

Return of the head command on the Life expectancy Dataset

After successfully reading our data and creating our dataframe, we can start getting some information out of it with two simple methods:

info: the info method returns the number of rows in the dataframe, the number of columns, the name of each column of the dataframe along with the number of non-null values of such column, and the data type of each column.

Results of using the info method on both dataframes

2. describe: the describe method returns some useful statistics about the numeric data in the dataframe, like the mean, standard deviation, maximum and minimum values, and some percentiles.

Information returned by the describe method of the Life Expectancy DataFrame

The next step after getting this global view of our data is learning how to access specific records of our dataframe. Like a python list, pandas dataframes can be sliced, using exactly the same notation as for the lists.

So if we want to select the first 10 rows of our dataframe, we could do something like:

First 10 rows of data

Indexing and selecting data

With loc and iloc you can do practically any data selection operation on DataFrames you can think of.

1. loc is label-based, which means that you have to specify rows and columns based on their row and column labels. 2. iloc is integer index based, so you have to specify rows and columns by their integer index.

After successfully understanding theory behind the loc and iloc , Let’s get started to implement it.

Average Life Expectancy over 15 Years in Afghanistan.

2. Highest Life Expectancy in Developed Country over 15 years

3. Maximum polio in the countries over 15 years.

4. Maximum percentage expenditure of the Developing country over 15years.

5. Lowest Adult Mortality in a particular country over 15 years.

Any groupby operation involves one of the following operations on the original object. They are −

Splitting the Object

the Object Applying a function

a function Combining the results

6. Showing data of all Country having avg total expenditure, sum of polio and standard deviation of life expectancy with the method called groupby —

We have seen what Pandas is, and some of its most basic uses. In the following posts we will see more complex functionalities and dig deeper into the workings of this fantastic library!

That is all, I hope you liked the post. Feel Free to follow me on Medium