Intro to Pandas and Numpy: Basic Tutorials Part 6

Abhishek V Suryavanshi
4 min readMar 19, 2018

--

Pandas

Pandas is one of the data centric python packages that makes importing and analyzing data much easier. Pandas is build on Numpy and matplot which makes data manipulation and visualization more convinient.

Importing Pandas

Before importing on your platform,we need to first install it.The installation guide to pandas can be found here->https://pandas.pydata.org/pandas-docs/stable/install.html.

import pandas as pd

Loading data

from .csv
rev=pd.read_csv("ifg.csv")
from dictionary
rev=pd.Dataframe(dict)
from database[docs]
from pandas.io import sql
import sqlite3
conm=sqlite3.connect(/Users/gjreda/Dropbox/gregreda.co)
que="select * from towed where make='TOWER'"
res=sql.read_sql(que,con=conm)
res.head()

Head and tail command

rev.head()
rev.tail()

The head command is used to return the first N rows in the data frame whereas tail is used to get the last N rows.

Dataframe

Dataframe is a 2-dimensional labeled data structure with columns of potentially different types.

df = pd.DataFrame([[y, x1_1, x2_1, ...], [y, x1_2, x2_2, ...], ... ])
df.columns = ['class', 'x1', 'x2', ...]

Basic description of a dataframe: df.shape

Description of dataframe: df.describe()

List column types: df.dtypes

List unique values in a column: df['columnName'].unique()

Numpy

Numpy is that library for computing in python.It provides high performance multidimensional array object and tools for working with arrays.

Importing

import numpy as np

a = np.array([1, 2, 3])
print(type(a))
print(a.shape)
print(a[0], a[1], a[2])
a[0] = 5
print(a)

b = np.array([[1,2,3],[4,5,6]])
print(b.shape)
print(b[0, 0], b[0, 1], b[1, 0])

Creating arrays

import numpy as np

a = np.zeros((2,2))
print(a)


b = np.ones((1,2)) # Create an array of all ones
print(b) # Prints "[[ 1. 1.]]"

c = np.full((2,2), 7) # Create a constant array
print(c) # Prints "[[ 7. 7.]
# [ 7. 7.]]"

d = np.eye(2) # Create a 2x2 identity matrix
print(d) # Prints "[[ 1. 0.]
# [ 0. 1.]]"

Array indexing

import numpy as np

# Create the following rank 2 array with shape (3, 4)
# [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])

# Use slicing to pull out the subarray consisting of the first 2 rows
# and columns 1 and 2; b is the following array of shape (2, 2):
# [[2 3]
# [6 7]]
b = a[:2, 1:3]

# A slice of an array is a view into the same data, so modifying it
# will modify the original array.
print(a[0, 1]) # Prints "2"
b[0, 0] = 77 # b[0, 0] is the same piece of data as a[0, 1]
print(a[0, 1]) # Prints "77"
import numpy as np

# Create the following rank 2 array with shape (3, 4)
# [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])

# Two ways of accessing the data in the middle row of the array.
# Mixing integer indexing with slices yields an array of lower rank,
# while using only slices yields an array of the same rank as the
# original array:
row_r1 = a[1, :] # Rank 1 view of the second row of a
row_r2 = a[1:2, :] # Rank 2 view of the second row of a
print(row_r1, row_r1.shape) # Prints "[5 6 7 8] (4,)"
print(row_r2, row_r2.shape) # Prints "[[5 6 7 8]] (1, 4)"

# We can make the same distinction when accessing columns of an array:
col_r1 = a[:, 1]
col_r2 = a[:, 1:2]
print(col_r1, col_r1.shape) # Prints "[ 2 6 10] (3,)"
print(col_r2, col_r2.shape) # Prints "[[ 2]
# [ 6]
# [10]] (3, 1)"

Datatypes

import numpy as np

x = np.array([1, 2]) # Let numpy choose the datatype
print(x.dtype) # Prints "int64"

x = np.array([1.0, 2.0]) # Let numpy choose the datatype
print(x.dtype) # Prints "float64"

x = np.array([1, 2], dtype=np.int64) # Force a particular datatype
print(x.dtype)

You can find me on Twitter, connect with me on Linkedin here

Subscribe to our Newsletter for a weekly curated reads of Deep Learning and Computer Vision Articles

Help us work the the Late Night Shifts by buying us a cup of coffee

--

--

Abhishek V Suryavanshi

Deep learning enthusiast |https://www.linkedin.com/in/suryavanshi18/