Python – Data Operations

Python – Data Operations

Python – Data Operations

Python handles data of various formats mainly through the two libraries, Pandas and Numpy. We have already seen the important features of these two libraries in the previous chapters. In this chapter we will see some basic examples from each of the libraries on how to operate on data.

Data Operations in Numpy

The most important object defined in NumPy is an N-dimensional array type called ndarray. It describes the collection of items of the same type. Items in the collection can be accessed using a zero-based index. An instance of ndarray class can be constructed by different array creation routines described later in the tutorial. The basic ndarray is created using an array function in NumPy as follows −

numpy.array

Following are some examples on Numpy Data handling.

Example 1

# more than one dimensions
import numpy as np
array = np.array([[11, 12], [13, 14]])
print array

The output is as follows −

[[11, 12]
[13, 14]]

Example 2

# minimum dimensions
import numpy as np
array = np.array([11, 12, 13,14,15], ndmin = 2)
print array

The output is as follows −

[[11, 12, 13, 14, 15]]

Example 3

# dtype parameter
import numpy as np
array = np.array([21, 22, 23], dtype = complex)
print array

The output is as follows −

[ 21.+0.j,  22.+0.j,  23.+0.j]

Data Operations in Pandas

Pandas handles data through Series,Data Frame, and Panel. We will see some examples from each of these.

Pandas Series

Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). The axis labels are collectively called index.
A pandas Series can be created using the following constructor −

pandas.Series( data, index, dtype, copy)

Example

Here we create a series from a Numpy Array.

#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
array = np.array(['z','y','x','w'])
series = pd.Series(array)
print series

Its output is as follows −

0   z
1   y
2   x
3   w
dtype: object

Pandas DataFrame

A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. A pandas DataFrame can be created using the following constructor −

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

Let us now create an indexed DataFrame using arrays.

import pandas as pd
data = {'Name':['Ram', 'Sham', 'Ghansham', 'Laxman'],'Age':[22,25,25,29]}
df = pd.DataFrame(data, index=['index1','index2','index3','index4'])
print df

Its output is as follows −

Age    Name
index1    22      Ram
index2    25     Sham
index3    25    Ghansham
index4    29    Laxman

Pandas Panel

A panel is a 3D container of data. The term Panel data is derived from econometrics and is partially responsible for the name pandas − pan(el)-da(ta)-s.
A Panel can be created using the following constructor −

pandas.Panel(data, items, major_axis, minor_axis, dtype, copy)

In the below example we create a panel from dict of DataFrame Objects

#creating an empty panel
import pandas as pd
import numpy as np
D = {'Item1' : pd.DataFrame(np.random.randn(4, 3)),
'Item2' : pd.DataFrame(np.random.randn(4, 2))}
panel = pd.Panel(D)
print panel

Its output is as follows −

<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 4 (major_axis) x 5 (minor_axis)
Items axis: 0 to 1
Major_axis axis: 0 to 3
Minor_axis axis: 0 to 4
Python – Matplotlib (Prev Lesson)
(Next Lesson) Python – Data Cleansing