Pandas Tutorial – Learn Pandas Library

Pandas is a python library used for data manipulation and analysis. In this Pandas Tutorial, we will learn about the classes available and the functions that are used for data analysis.

Install Pandas Library

To install pandas, use the following pip command.

pip install pandas

Import Pandas

To import pandas python library, use the following statement in your program before using pandas classes.

import pandas

Usually an alias is used for pandas while using in a program. Use the following import statement.

import pandas as pd

Pandas Datastructure

Pandas has two types of Datastructures to deal with the data. They are:

  1. DataFrame
  2. Series

Pandas DataFrame

Pandas DataFrame is similar to R DataFrame. It stores two dimensional data with the structure similar to that of a table in databases.

Following is an example DataFrame.

names  us  india  china
0   Google  68     84     78
1    Apple  74     56     88
2  Samsung  77     73     82
3  OnePlus  78     69     87

Rows can be accessed using index and columns can be accessed using column labels.

To initialize a DataFrame, you can use pandas DataFrame() class. The syntax of DataFrame() is:

DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)

where

  • data is ndarray (structured or homogeneous), Iterable, dict, or DataFrame
  • index to use for resulting frame.
  • columns to use as labels for the resulting frame.
  • dtype is the data type that has to be forced on the data.
  • copy flag to enable copying data from inputs.

Create a DataFrame

Let us create a dataframe from dictionary.

example.py

import pandas as pd

mydictionary = {'names': ['Google', 'Apple', 'Samsung', 'OnePlus'],
	'us': [68, 74, 77, 78],
	'india': [84, 56, 73, 69],
	'china': [78, 88, 82, 87]}

# create dataframe
df_marks = pd.DataFrame(mydictionary)

print(df_marks)

Output

names  us  india  china
0   Google  68     84     78
1    Apple  74     56     88
2  Samsung  77     73     82
3  OnePlus  78     69     87

Pandas DataFrame Operations

  • Pandas DataFrame – Change Column Names
  • Pandas DataFrame – Delete Column
  • Pandas DataFrame – Get First Row
  • Pandas DataFrame – Get Last Row
  • Pandas DataFrame – Filter Rows
  • Pandas DataFrame – Reset Index