Data Operations using Python

Neha Kumawat

a year ago

In this article, we will try to see how we can perform some operations on our data frame using python libraries like Pandas and Numpy. We all know how important and useful these python libraries are while we are performing an Exploratory Data Analysis or any Data Science projects.
Data Operations
Data Operations
Here we will try to learn some basic examples from each of the two libraries and see how they help us to operate on the data.

Operations on data using Numpy

Numpy package is a very important library provided by python used for any mathematical operation is to be done. This Package is very important for performing any Machine learning and Deep Learning tasks.
Numpy stands for N-dimensional array called ndarray. It describes the collection of items of the same type. Items in any collections can be accessed using a zero-based index. An instance of ndarray class can be constructed by different array creation.

How to create ndarray?

Let’s see with some examples, how basic ndarray is created using an array function in Numpy.

Below are some examples of Numpy Data handling.

Example 1
# more than one dimensions
import numpy as np 

arr = np.array([[4, 6], [7, 8]]) 

[[4 6]

 [7 8]]
Example 2
# minimum dimensions
import numpy as np 

arr = np.array([1, 2, 3, 4, 5], ndmin = 2) 

[[1 2 3 4 5]]
Example 3
# with dtype parameter
import numpy as np 

arr = np.array([1, 2, 3], dtype = complex) 

[1.+0.j 2.+0.j 3.+0.j]

Data Operations in Pandas

Python pandas library provides many functions to handles data through Series, Data Frame, and Panel.

Series in Pandas

Pandas Series is a one-dimensional labeled array which is capable of holding data of any type (integer, string, float, python objects, etc.). The axis labels are collectively known as index.
Let’s see how we can create a pandas series
pandas.Series( data, index, dtype, copy)
Now let’s take an example and understand it in a better way
How to create a series from a Numpy Array.
#import the pandas library and aliasing as pd
import pandas as pd

import numpy as np

data = np.array(['P','Q','R','S'])

series = pd.Series(data)

0    P

1    Q

2    R

3    S

dtype: object

DataFrame in Pandas

Pandas data frame is a two-dimensional data structure in python. Here, data is aligned in a tabular form in rows and columns.
Let’s see how we can create pandas DatFrame.
pandas.DataFrame( data, index, columns, dtype, copy)
Let’s see an example
pandas as pd

data =
{'Name':['Ram', 'Mohan', 'Amit', 'Ricky'],'Age':[23,25,29,36]}

df1 =
pd.DataFrame(data, index=['rank1','rank2','rank3','rank4'])


        Name  Age
rank1    Ram   23
rank2  Mohan   25
rank3   Amit   29
rank4  Ricky   36
For more blogs/courses on data science, machine learningartificial intelligence, and new technologies do visit us at InsideAIML.
Thanks for reading…  

Submit Review